445,797 Members | 1,794 Online
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

# diff between array name and its base adress

 P: n/a if we have a declaration int arr[] = { 1,2,3,4,5}; int *ptr; what is the diff betn ptr = &a and ptr = a ; when i do ptr = &a ; or ptr = a ; and then ptr += 2 ; then *ptr points to 3 in both cases Basically , my doubt is whether the statement " array name is same as the address of array " true in all cases without exception and can both be used interchangebly ..?? kind regards rahul Sep 26 '06 #1
4 Replies

 P: n/a ra*******************@gmail.com wrote: if we have a declaration int arr[] = { 1,2,3,4,5}; int *ptr; what is the diff betn ptr = &a and ptr = a ; See the thread 'C Strings not returning from a function' from earlier today. -- Ian Collins. Sep 26 '06 #2

 P: n/a ra*******************@gmail.com wrote: if we have a declaration int arr[] = { 1,2,3,4,5}; int *ptr; what is the diff betn ptr = &a and ptr = a ; You will get warning in 'ptr = &a' "warning: initialization from incompatible pointer type" and you wont get warning in 'ptr = a' Basically, &a return pointer to array of integer of size 5. And , a return pointer to integer. We can experiment this by following statements; ( (unsigned long )(&a + 1) - (unsigned long )(&a) ) , this will return 5 * 4 = 20 (Assume : sizeof(int )= 4 ) and ( (unsigned long )(a + 1) - (unsigned long )(a) ) will return 4; #include int main() { int a[5]; int *ptr = a; int *ptr1 = &a ;/*warning: initialization from incompatible pointer type*/ int (*ptr2)[5] = &a; printf("%d\n",(int) ( (unsigned long )(&a + 1) - (unsigned long )(&a) ) ); printf("%d\n",(int) ( (unsigned long )(a + 1 ) - (unsigned long )(a ) ) ); return 0; } Sep 26 '06 #3

 P: n/a ra*******************@gmail.com wrote: if we have a declaration int arr[] = { 1,2,3,4,5}; int *ptr; what is the diff betn ptr = &a and ptr = a ; Their types. See c-faq.com, sec 6.12. when i do ptr = &a ; or ptr = a ; and then ptr += 2 ; then *ptr points to 3 in both cases Basically , my doubt is whether the statement " array name is same as the address of array " true in all cases without exception and can both be used interchangebly ..?? Array name in expression is the address its initial element in some context. Sep 26 '06 #4

 P: n/a On 25 Sep 2006 22:23:27 -0700, "ra*******************@gmail.com"

### This discussion thread is closed

Replies have been disabled for this discussion.