-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 !!

2
  • values *ptr and ptr are different, cause "%d" and "%u" used in printf Commented Dec 6, 2018 at 8:28
  • 1
    Do not post images of text. Commented Dec 6, 2018 at 8:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.