Array question...i'm really confused...

Started by
23 comments, last by law_order 19 years, 6 months ago
&multi is the address that multi is stored at. multi is a pointer to a block of memory (ie, your array). However, they are both the same location in memory.

[Edited by - ontheheap on October 17, 2004 8:38:41 AM]
Advertisement
Quote:Original post by ontheheap
&multi is the address that multi is stored at. multi is a pointer to a block of memory (ie, your array).


EXACTLY!So,why when i'm typing &multi i'm taking the adress of the first array element?

for example here:

" """ #include <stdio.h>





main() { int a[3]; printf("%d", &a); printf("%d", &a[0]); } """"


both printf(s) output the same value...
No, multi is NOT a pointer to the array, it IS the array.

For a second, forget about arrays and let's think about a single variable, a single int for example.
int foo;

foo is not a pointer to the integer value, it is the integer itself. Saying foo in the code gives you the value of foo, and &foo gives you the address of to the value.

Now just think about arrays as several values grouped together instead of just a single value.
int foo[5];

foo still isn't a pointer, it is the name for the array itself. Instead of the name representing a single integer (as above), it now represents five integers. Just like in the above example, &foo gives you the address of what it represents, which is the array. The address of the array is the same as the address of the first element in the array.
So why is the pointer in law_order's example located at exactly the same address as the array it points to? That would mean the pointer and the beginning of the array shares address, which makes no sense, and changing the value of the first element would also change the value of the pointer, making the pointer point somewhere else, making the array useless.

However, the name of the array can be implicitly converted to a pointer of the type of object the array contains. But this doesn't mean the name of the array actually IS a pointer, just that you can get the pointer from it.

edid: Ehm, did you delete your post?
Yup. I was wrong. [grin]

I've wrote a small program to see what would happen and you were correct.
Thank you Brother bo....i finally underestood your points...

but one last question please...

we say that ar[n] is equivalent to *(ar+n).What we actually do here,is incrementing a pointer..right?
why here ar is a pointer?(if it is of course...)

thanks so much...again...
Quote:Original post by Brother Bob
So why is the pointer in law_order's example located at exactly the same address as the array it points to? That would mean the pointer and the beginning of the array shares address, which makes no sense, and changing the value of the first element would also change the value of the pointer, making the pointer point somewhere else, making the array useless.

However, the name of the array can be implicitly converted to a pointer of the type of object the array contains. But this doesn't mean the name of the array actually IS a pointer, just that you can get the pointer from it.



that makes sense...although a little bit confusing...isn't it?
so...you mean that the array's name isn't a real pointer,but sometimes the compiler treats it as a pointer?
Quote:Original post by law_order
that makes sense...although a little bit confusing...isn't it?
so...you mean that the array's name isn't a real pointer,but sometimes the compiler treats it as a pointer?

Sort of. Consider this example.
int foo[5];int *bar = foo;

foo is not a pointer, as described above, but bar is. When you assign bar the value of foo, the compiler implicitly convert the name of the array to a pointer, and this (temporary if you like) pointer is used for to assign bar a new value.

Quote:Original post by law_order
we say that ar[n] is equivalent to *(ar+n).What we actually do here,is incrementing a pointer..right?
why here ar is a pointer?(if it is of course...)

Yes, you increment the value of the pointer and dereference it. Whether ar is a pointer or an array doesn't really matter, since you can get the pointer from the array name.
""""Original post by law_order
we say that ar[n] is equivalent to *(ar+n).What we actually do here,is incrementing a pointer..right?
why here ar is a pointer?(if it is of course...)



Yes, you increment the value of the pointer and dereference it. Whether ar is a pointer or an array doesn't really matter, since you can get the pointer from the array name."""""""

I didn't really underestand that...do you mean that again the compiler converts ar to a pointer?
What, exactly, the compiler does isn't really important. But for the sake of understanding why ar[n] = *(ar + n), you can think of it like that, yes.

This topic is closed to new replies.

Advertisement