use of "->" on arrays?

P

Paminu

I thought that "->" was only used with pointers. Why is it that it is also
allowed to use it on arrays??

struct test {
int a;
int b;
};

struct test array[5];

void wierd()
{
array->b = 200;
printf("array->b: %d\n", array->b);

}

int main()
{
wierd();
return 1;
}


It is not allowed if I do something like:

array[1]->b = 200;

so how do I know which element that gets field b set to 200 when I type:

array->b = 200;
 
R

Richard Tobin

struct test {
int a;
int b;
};

struct test array[5];

void wierd()
{
array->b = 200;
printf("array->b: %d\n", array->b);

}

A nice example... In most contexts, an array name is converted to a
pointer to its first element, so array->b is equivalent to (*array).b
which is equivalent to array[0].b.

-- Richard
 
C

Charles M. Reinke

Paminu said:
I thought that "->" was only used with pointers. Why is it that it is also
allowed to use it on arrays??

struct test {
int a;
int b;
};

struct test array[5];

void wierd()
{
array->b = 200;
printf("array->b: %d\n", array->b);

}

int main()
{
wierd();
return 1;
}


It is not allowed if I do something like:

array[1]->b = 200;

so how do I know which element that gets field b set to 200 when I type:

array->b = 200;

hologram>cat my_array.c
#include <stdio.h>

struct test
{
int a;
int b;
}; /* struct test */

struct test array[5];

void wierd(void) /* BTW, itz spelled "weird" */
{
int i;

array->b = 200;
printf("array->b: %d\narray[0].b: %d\n\n", array->b, array[0].b);
for(i=0; i<5; i++)
{
(array+i)->a = i;
(array+i)->b = i * i;
} /* for i */
} /* wierd */

int main(void)
{
int i;

wierd();
for(i=0; i<5; i++)
{
printf("(array+%d)->a: %d; (array+%d)->b: %d\n", i, (array+i)->a, i,
(array+i)->b);
printf("array[%d].a: %d; array[%d].b: %d\n\n", i, array.a, i,
array.b);
} /* for i */

return 0;
} /* main */
hologram>gcc -Wall -ansi -pedantic -o my_array.exe my_array.c
hologram>my_array.exe
array->b: 200
array[0].b: 200

(array+0)->a: 0; (array+0)->b: 0
array[0].a: 0; array[0].b: 0

(array+1)->a: 1; (array+1)->b: 1
array[1].a: 1; array[1].b: 1

(array+2)->a: 2; (array+2)->b: 4
array[2].a: 2; array[2].b: 4

(array+3)->a: 3; (array+3)->b: 9
array[3].a: 3; array[3].b: 9

(array+4)->a: 4; (array+4)->b: 16
array[4].a: 4; array[4].b: 16

Bottom line: there are a couple of different ways of coding the same thing
due to the *similar* properties of arrays and pointers in C. However you
will probably want to refer to section 6 of the C FAQ
(http://www.faqs.org/faqs/C-faq/faq/) to learn what the similarities (and
most important, the differences) really are.

-Charles
 
G

g.kanaka.raju

Paminu said:
I thought that "->" was only used with pointers. Why is it that it is also
allowed to use it on arrays??

struct test {
int a;
int b;
};

struct test array[5];

void wierd()
{
array->b = 200;
printf("array->b: %d\n", array->b);

}

int main()
{
wierd();
return 1;
}


It is not allowed if I do something like:

array[1]->b = 200;

so how do I know which element that gets field b set to 200 when I type:

array->b = 200;

You are correct that "->" is only used with pointers. In your exampale
"array" is nothing but a pointer. It's the base address of the array.
In other words, address of array[0].
Following is not allowed as the operator -> expects a pointer.
array[1]->b = 200;
But you can always right (array+1)->b = 200; Here array+1 is a pointer
to second element of the array.

Regards,
Raju
 
K

Keith Thompson

Paminu said:
I thought that "->" was only used with pointers. Why is it that it is also
allowed to use it on arrays?? [...]
struct test array[5];
[...]
You are correct that "->" is only used with pointers. In your
exampale "array" is nothing but a pointer. It's the base address of
the array. In other words, address of array[0].

That's both right and wrong. "array" itself is an array object, not a
pointer. The name "array", when used in an expression, is converted
to a pointer value.
Following is not allowed as the operator -> expects a pointer.
array[1]->b = 200;
But you can always right (array+1)->b = 200; Here array+1 is a pointer
to second element of the array.

Yes -- but of course it's much less clear than "array[1].b = 200;".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top