1 votes 1 votes #include <stdio.h> main() { int arr[5]; printf("%p,%p\n",arr,&arr); } How do both print the same address? Programming in C array-of-pointers + – K ANKITH KUMAR asked Aug 10, 2018 K ANKITH KUMAR 1.2k views answer comment Share Follow See all 22 Comments See all 22 22 Comments reply MiNiPanda commented Aug 10, 2018 i edited by MiNiPanda Aug 10, 2018 reply Follow Share Yes but arr+1 and &arr+1 will give different results. arr is the base address of the array i.e. the address of arr[0]. So arr+1 means address of the next element i.e. address of ar[1] which will be &(arr[0])+size of int. &arr is the address of the array. &arr+1 means incrementing the address by the size of the array i.e. &arr+1 = &arr + (size of the array *size of int) = &arr+ (5*size of int) Let me know if you get it.. 4 votes 4 votes Soumya29 commented Aug 10, 2018 i edited by srestha Aug 18, 2019 reply Follow Share Type of $arr$ is $int \ *$ While the type of $\& arr$ is $int (*) [5].$ $\text {Array_name gives the address of}\ 1^{st} \ \text{element of the array. }$ $\text{&Array_name gives the address of whole array}$ Address of array is same as address of its first element. 5 votes 5 votes Bhagyashree Mukherje commented Aug 10, 2018 reply Follow Share @MiNiPanda arr+1 will give address of arr[1] and &arr+1 will give base address of arr incremented by 1.Am i right? Sorry.just read your comment.Thanks 0 votes 0 votes MiNiPanda commented Aug 10, 2018 reply Follow Share &arr+1 will give base address of arr + size of the arr*size of each element in the arr. See the picture, the base address is 6487600. When is do &ar+1 it becomes 6487620 because the base address is now increased by 5*4B [ size of array =5 elements, size of int variable is 4B]. 0 votes 0 votes Bhagyashree Mukherje commented Aug 10, 2018 reply Follow Share yes got it now.Thank you 0 votes 0 votes Soumya29 commented Aug 10, 2018 reply Follow Share @MiNiPanda, %p modifier is used for printing the addresses. If you're on a system with 32-bit integers but 64-bit pointers, if you attempt to print a pointer with any of %d, %u, or %x, you'll get the wrong result. 1 votes 1 votes MiNiPanda commented Aug 10, 2018 reply Follow Share But then &arr+1 should give what result according to you? 0 votes 0 votes Soumya29 commented Aug 10, 2018 reply Follow Share &arr+1 will give (base address of arr + (no._of_elements_in_the_array*size of int)) "base address of arr " part will make the difference if pointer sizes and int sizes are different on a machine. 0 votes 0 votes MiNiPanda commented Aug 10, 2018 i edited by MiNiPanda Aug 10, 2018 reply Follow Share Okay..yes..I also knew the same..but can you please check the picture? When i use %u, it is working according to the formula you mentioned i.e. base address is added with +20. But why is it different in case of %p? You said using %u on 32 bit integer machines will give error but here i see the opposite.. Is it because in this formula we are not dealing with any pointer size? 0 votes 0 votes Soumya29 commented Aug 10, 2018 reply Follow Share What's the problem in it? Above code worked fine with both %u and %p. And both giving equal address which means both the pointer and int have same sizes. 0 votes 0 votes srestha commented Aug 10, 2018 reply Follow Share While the type of &arr is int(∗)[5]. @Soumya Can u elaborate this line more? 5 means size also defined rt? 0 votes 0 votes Soumya29 commented Aug 10, 2018 reply Follow Share @Srestha, I wrote it wrt to the above question. In general $ \& \text{array_name has type int} (* )[\ ]. $ 0 votes 0 votes Arjun commented Aug 10, 2018 reply Follow Share @Soumya only if array is for int type :) Above code worked fine with both %u and %p. And both giving equal address which means both the pointer and int have same sizes. It doesnt mean that. It can also work fine if the pointer value does not take more bits than given by int - actual pointer size can be still different. 1 votes 1 votes Soumya29 commented Aug 10, 2018 reply Follow Share Ohh. yes, @Arjun sir. Thank you. I missed that point :) If int size is 32 bits and pointer size is 64 bits then %p and %u both will give same results for $0 \ to \ 2^{32}-1$ but the behaviour will be undefined for $2^{32} \ to \ 2^{64}-1.$ 1 votes 1 votes srestha commented Aug 10, 2018 reply Follow Share @Arjun Sir I couldnot understand how pointer size differ here? pointer must be either int or void or char. I mean it couldnot stand without type is defined So, here already pointer defined as int How actual pointer size and integer pointer (as pointer is defined int here) will be different? 0 votes 0 votes MiNiPanda commented Aug 10, 2018 reply Follow Share What's the problem in it? Soumya29 For arr and &arr both gave 000000000062FE30 for %p and for %u both gave 6487600. %p %u arr+1 000000000062FE34 6487604 &arr+1 000000000062FE44 6487620 If you notice the table carefully then you can see that arr+1 increases by 4B(i.e. from 000000000062FE30 to 000000000062FE34) on %p but &arr+1 increases by 14B (i.e. from 000000000062FE30 to 000000000062FE44)on %p. The incrementation in case of &arr+1 with %u is 20B which is actually expected. 0 votes 0 votes Soumya29 commented Aug 10, 2018 reply Follow Share @MiNiPanda, It's Hex not decimal. You are observing the difference in decimal. $0x62FE30+0x000014 =0x62FE44$ $(20)_{10}=(14)_{16}$ 2 votes 2 votes MiNiPanda commented Aug 10, 2018 reply Follow Share Oh sorry!!! How could i forget it! Thanks :P 1 votes 1 votes srestha commented Aug 10, 2018 reply Follow Share @Soumya See this program https://ideone.com/f543lL #include <stdio.h> int main() { int arr[] = { 3, 5, 6, 7, 9 }; int *p = arr; int (*ptr)[5] = &arr; printf("p = %p, ptr = %p\n", p, ptr); printf("*p = %d, *ptr = %p\n", *p, *ptr); printf("sizeof(p) = %lu, sizeof(*p) = %lu\n", sizeof(p), sizeof(*p)); printf("sizeof(ptr) = %lu, sizeof(*ptr) = %lu\n", sizeof(ptr), sizeof(*ptr)); return 0; } Sizeof(p) and sizeof(ptr) is same for this program then how int size is 32 bit and pointer size 64 bit in same program? 0 votes 0 votes Arjun commented Aug 10, 2018 reply Follow Share @srestha why couldn't you print sizeof(int) and sizeof(int*)? 1 votes 1 votes srestha commented Aug 10, 2018 reply Follow Share in an array sizeof(int), how possible it? always there is a pointer dereference isn't it? 0 votes 0 votes srestha commented Aug 11, 2018 reply Follow Share I understood what u mean by this. Integer has it's own size and when integer contains a pointer it has different size. That means pointer itself must contain some size 0 votes 0 votes Please log in or register to add a comment.