Hey there. I'm new to here so bear with me. The question:
If you have a declaration such as: int a[15];
which experssion would be a pointer to the 3rd element?
a+2
or
$(a+2)
I'm a little confused. any info would be great
Printable View
Hey there. I'm new to here so bear with me. The question:
If you have a declaration such as: int a[15];
which experssion would be a pointer to the 3rd element?
a+2
or
$(a+2)
I'm a little confused. any info would be great
Code:int *ptr;
ptr = a + 2;
/* or */
ptr = &a[2];
Dave already posted the answer.
Quote:
Originally Posted by Hoser83
I think Dave pegged it as the first choice there.
The second choice looks like valid C, but is referencing the 4th element of the array, as array indexes (indices???) always start at 0.
The third choice is also valid C, but again, references the 4th element.
The last choice is not valid C.
oh, sorry guys. my bad. I didn't know if the
experssion needed the ptr = a + 2 or if it was just a + 2.
I got ya now.
Thanks a lot for the help. I really appreciate it.
the expression to reference the 3rd element is "a+2", if you then want to assign that to "ptr" you will need to type "ptr=a+2".