While dereferencing pointer to an array I am getting same address as that of pointer to an array

97 views Asked by At
#include<stdio.h>

int main(void)
{
   int arr[5]={1,2,3,4,5};
   int (*ptr)[5]=&arr;

   printf("ptr=%p\n",ptr);    i am not getting the diff btw both statements
   printf("*ptr=%p\n",*ptr);

   return 0;
}

output:- 
ptr=0xbf8f8178
*ptr=0xbf8f8178

I know dereferencing the pointer to an array we get the array name and array name denotes the base address then what's the diff between both statements

2

There are 2 answers

0
kapil On BEST ANSWER

the first pointer in printf is pointer to pointer

the second pointer in printf is pointing to first element pointed by the first pointer

but the types of two pointers are different

enter image description here

see this image

source:https://www.eskimo.com/~scs/cclass/int/sx9b.html

0
newacct On

The two pointers have the same address but have different types.

ptr is a pointer to an array of 5 ints.

*ptr is an array of 5 ints. However, when an expression of "array of T" type is used in any context except sizeof or &, it is automatically converted to an expression of "pointer to T" type, pointing to the array's first element. In this case, it becomes a pointer to int.

Obviously an array starts at the same address as its first element.