0

I was writing this program -

#include<stdio.h>

void main()
{ 
    int arr[20]; 
    arr[0]=22; 
    arr[1]=23; 
    int (*p)[20]=&arr; 
    printf("address in p :%u:\n",p);
    printf("address in *p:%u:\n",*p);
}

The Output of this code is same for p and *p ! So far as I know *p is holding the base address of arr which is nothing but arr[0]!!! So *p should have give the output 22 ! But it's showing the same memory address like p is showing. Please tell me why this is happened? What is the reason behind it.
Codepad Site Link : http://codepad.org/LK7qXaqt

haccks
  • 104,019
  • 25
  • 176
  • 264
Satyaki
  • 751
  • 2
  • 10
  • 25

3 Answers3

5

Because p and *p points to same memory location only there types are different

    +-+-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+-+
arr [                        ]
  p [                        ]
(*p)[ ]

If you print p+1 and *p + 1 you should see the difference

int (*p)[20] declare p as pointer to array of size 20 of type int so at the same time *p is the pointer to the first element of the array.

Address of first element and of whole array would be same.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • But *p should give the actual value stored in the address holding in the pointer ? Is not it? – Satyaki Jul 10 '14 at 11:19
  • `p` is address of array, `*p` is address of first element (i.e. `int`) and `**p` is the actual value stored in zeroth index. Moreover `p+1` is address beyond array and `*p+1` is address beyond 0th element i.e. of element at first index. – Mohit Jain Jul 10 '14 at 11:21
  • @MohitJain; `*p[i]` will not give `arr[i]`. – haccks Jul 10 '14 at 11:36
  • @haccks Alias for `arr[i]` would be `(*p)[i]` because `[]` couples righter than `*` according to c precedence table. And I did not say `*p[i]` will give `arr[i]`. Did I imply it somewhere by mistake? – Mohit Jain Jul 10 '14 at 11:40
  • @MohitJain; I thought about this --> `*p[ ]` – haccks Jul 10 '14 at 11:43
  • @haccks Sorry If I was not clear. What I want to say is, `*p` points to 0th index (starting with `[`) and type of `*p` is such that it ends where the 0th element ends (shown with `]`). I would surround `*p` in a parenthesis to make it clearer. – Mohit Jain Jul 10 '14 at 11:46
5

p is a pointer to an array of 20 integers. Address of first byte of array is said to be the address of the array. Dereferencing it will give the entire array itself. Therefore *p represents the array arr, so you can think of *p as an array name.
As array names are converted to pointers to its first element when passed to a function, *p is decayed to pointer to first element of arr. Therefore

printf("address in *p: %p:\n", (void*)*p);  

will print the address of first element of array arr while

printf("address in p: %p:\n", (void*)p);  

will print the address of the entire array (i.e first byte of the array). Since value of first byte and first element is same, that's why both are printing the same address.

For detailed explanation: Stack Overflow What exactly is the array name in c?

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
0

Yes it is possible to apply the address operator to an array, although it seems redundant. The result p is a pointer to array. Such a thing is under normal circumstances rarely encountered. As @Mohit pointed out correctly, the object it points to is the whole array, which implies that sizeof(*p) should be the size of the array (not of its first element).

*p logically is the array then; as usual it decays to a pointer to its first element (which is an int) when passed as an argument, e.g. to printf. Since it's the first element its location in memory is the location of (the beginning of) the whole array.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62