Jason
Jason

Reputation: 3

About variable not a pointer printed through %p

I am a student who is studying pointer unit.

I posted this message to get a lesson from my seniors because I was ignorant and couldn't understand it well.

First of all, thank you so much for reading my article.

int main(){
int a;
int* pa;

pa = &a;
a = 1;

printf ("'a' something: %p\n", a);
printf ("'a' address : %p\n", &a);
printf ("'a' value : %d\n\n", a);

printf ("'pa' address value : %p\n", pa);
printf("'*pa' address value? : %p\n", *pa);


return 0;
}

result

'a' of what?: 0x1

'a' address: 0x7ffd407638b8

'a' value : 1

'pa' address : 0x7ffd407638b8

'*pa' address : 0x1

Here's the question.

Question1. I would like to ask if the value printed through the %p format means something, even though variable a is not a pointer.

Question2. I would like to ask you if there is a different reason why the result of printing the 'pa' through %p format and the result of printing '*pa' %p format even the same pointer called p.

Question3. If I understood the above two questions, I don't need to ask about this.*why variable a is the same as the 'a' printed in the %p format and pointer '*pa' printed in the %p format.

Thank you.

Thank you so much for taking your time to read my article.

Upvotes: 0

Views: 158

Answers (3)

1. Question

"I would like to ask if the value printed through the %p format means something, even though variable a is not a pointer."

It is undefined behavior to print a non-pointer value with %p. Strictly seen it is even not compliant to the standard, if you don't cast the pointer to void * as relative argument.

The C standard states (emphasize mine):

"p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner."

Source: C18, 7.21.6.1/8

"If a conversion specification is invalid, the behavior is undefined.288) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined."

Source: C18, 7.21.6.1/9


2. Question

"I would like to ask you if there is a different reason why the result of printing the pa through %p format and the result of printing *pa with %p format even the same pointer called p."

You only can use the former method to be proper. The latter invokes undefined behavior as described above.


3. Question:

"If I understood the above two questions, I don't need to ask about this.*why variable a is the same as the a printed in the %p format and pointer 'paprinted in the%p` format."

It is not the same.

Upvotes: 0

Jason
Jason

Reputation: 3

I couldn't say no more about all of you so kind. I was so fool, and Thanks so much.

So It's okay I just accept NO.1 it doesn't work normal? Cause, conversion specification is invalid, the behavior is undefined.

No.2 result of printing '*pa' through %p means itself's address.

and result of printing 'pa' through %p means value of 'a'(but I also got mistake that I used wrong conversion specification). If I want to fix it right. I should write

printf("'a' someting : %p\n", a); → printf ("'a' value : %d\n", a); == 1

printf("%p\n", *pa); → printf("%d\n", *pa); == 1

No.3

Yeah, it may print same result. Anyway It was wrong.

Cause I used conversion specification. So it should be change,

printf("'a' something: %p\n", a); → printf ("'a' value : %d\n", a); == 1

printf("%p\n", *pa); → printf("%d\n", *pa); == 1

Thanks for your teaching, and I hope what I understood must be right. Am I?

Upvotes: 0

Secto Kia
Secto Kia

Reputation: 969

  1. On most real-life systems a pointer is no different to any integer, just that it holds a values presenting a memory address, rather than a value representing a number.

  2. *pa is the value at where the pa pointer points, not the value of the pointer.

  3. Because of the above, *pa is the same as a, they are both the value of a, not the value of the pointer.

Upvotes: 1

Related Questions