0

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.

Jason
  • 3
  • 3
  • 10
    Printing a non-pointer with %p results in undefined behavior, and the results are formally unpredictable. – Raymond Chen Jul 04 '20 at 05:02
  • 1
    You're trying to print a memory address of a dereferenced pointer (in the last printf statement). – Rohan Bari Jul 04 '20 at 05:34
  • 1
    When you use `printf` (`fprintf`) the *conversion-specifier* much match the variable type you are trying to print or the behavior is undefined. [C11 Standard - 7.21.6.1 The fprintf function(p9)](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p9) "9 If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined." – David C. Rankin Jul 04 '20 at 05:45
  • 1
    If you are interested in a couple of answers that discuss pointer-basics in general, here are a few links that provide basic discussions of pointers that may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102). While the question titles discuss pointers-to-pointers and structs -- the answers, in part, discuss the basics of pointers. Good luck with your coding. – David C. Rankin Jul 04 '20 at 05:50
  • 2
    The type of the argument corresponding to the `%p` must be `void*`. – M. Nejat Aydin Jul 04 '20 at 06:43

3 Answers3

1
  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.

Secto Kia
  • 990
  • 4
  • 12
  • Thanks so much to you. I understood What I done must not be done. So shame, I should learn basic more and more. Thanks again! – Jason Jul 04 '20 at 08:06
0

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?

Jason
  • 3
  • 3
0

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.

  • 2
    I really want to say "Thanks a lot". I understood I did something wrong by doing undefined behavior by your comment. I was wonder 'Yeah, it is wrong to do this, but why it shows up that I didn't understood? If there's any meaning, should I know this?' But after read your comment I accepted What I done was totally wrong thing. Thanks so much to answering my Ques. – Jason Jul 04 '20 at 08:05