-4

I was trying to learn pointer to an array , but I'm not able to understand as to why *ptr and ptr print the same value

/*Here is the source code.*/

#include<stdio.h> 

int main() 
{ 
    int arr[] = { 3, 5, 6, 7, 9 }; 
    int *p = arr; 
    int (*ptr)[5] = &arr; 

    printf("p = %u, ptr = %u\n", p, ptr); 
    printf("*p = %d, *ptr = %d\n", *p, *ptr);  
    return 0; 
} 

and here is a snapshot of the output I got:How come ptr and *ptr return same values !!

gsamaras
  • 71,951
  • 46
  • 188
  • 305
mujtaba1747
  • 33
  • 1
  • 6

1 Answers1

1

Change the way you print to this:

printf("p = %p, ptr = %p\n", (void*)p, (void*)ptr); 
printf("*p = %d, *ptr = %p\n", *p, (void*)*ptr); 

since the format specifier p is used to print a pointer. Moreover, the pointer should be casted into a void pointer, when you intend to print it.

Possible output:

p = 0x7ffed5b62fd0, ptr = 0x7ffed5b62fd0
*p = 3, *ptr = 0x7ffed5b62fd0

where now you see that they are the same.

gsamaras
  • 71,951
  • 46
  • 188
  • 305