2
#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

Quentin
  • 62,093
  • 7
  • 131
  • 191
sam1006
  • 103
  • 1
  • 8
  • [This blog-post of mine](https://ghost.pileborg.se/2016/08/28/the-difference-between-arrays-decaying-to-pointers-and-pointers-to-arrays/) should hopefully clear things up a bit. Note what happens when you use pointer arithmetic on the pointers, where the difference between the two pointers will become very visible. – Some programmer dude Dec 24 '16 at 17:45
  • Got the answer, thank you – sam1006 Dec 29 '16 at 09:37
  • Both are undefined behaviour, `%p` requires `void *` – M.M Dec 31 '16 at 09:15

2 Answers2

0

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.

newacct
  • 119,665
  • 29
  • 163
  • 224
0

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

kapil
  • 625
  • 12
  • 20