Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-> Arrow operator with Arrays?

Tags:

arrays

c

Can you use the arrow operator with an array of type struct. For example:

struct {
char *foo;
} fooArray[10];

fooArray[0]->foo = ...
like image 353
Crystal Avatar asked Dec 01 '25 09:12

Crystal


2 Answers

No, you have to use fooArray[0].foo.

fooArray is a pointer that happens to point to the first element of an array. Using the [] is dereferencing that pointer at the given element so once applied it's no longer a pointer and the -> operator cannot be applied since that operator does both dereferencing and accessing a struct member.

like image 109
246tNt Avatar answered Dec 04 '25 06:12

246tNt


Remember the value of fooArray by itself is a pointer. You can dereference the pointer, add to it, ...

fooArray->foo; /* same as fooArray[0].foo */
(fooArray+3)->foo; /* same as fooArray[3].foo */
like image 20
pmg Avatar answered Dec 04 '25 06:12

pmg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!