Home>

I'm currently studying C language pointers and I wrote the following code.

=========================================
int main (void) {
int a;
int * b;
intc; a = 2; b =&a; c =&b; printf ("a's p is% p \ n", a); printf (p in"b is% p \ n", b); printf ("c p is% p \ n", c); printf ("a has value% d \ n", a); printf ("b value is% d \ n", * b); printf ("c value is% d \ n",c);

} This will return the following answer:

a p is 00000002
b p is 0061FF28
c p is 0061FF24
The value of a is 2
The value of b is 2

c is 2 This last line is output as * c instead of ** c as shown below. include<stdio.h>

int main (void) {
int a;
int * b;
int ** c;
a = 2;
b =&a;
c =&b;
printf ("a p is% p \ n", a);
printf ("b where p is% p \ n", b);
printf ("c p is% p \ n", c);
printf ("a value is% d \ n", a);
printf ("b is% d \ n", * b);
printf ("c is% d \ n", * c);

}

a p is 00000002
b p is 0061FF28
c p is 0061FF24
The value of a is 2
The value of b is 2

c is 6422312

What is the value of this returned 6422312?
I tried many things, but could not explain what this result output. If i understand
Thanks for your cooperation.

c
  • Answer # 1

      

    What is the value of 6422312

    Let's fix it to a hexadecimal number in the calculator app (because anything is fine).
    6422312 == 0x61FF28

    The output line has the following line:

      

    b p is 0061FF28

    % p is normally displayed in hexadecimal. b =&a;where b is the hexadecimal value,
    That is,6422312is the address of variable a displayed in decimal.

    When a mysterious number is displayed in decimal, it is often possible to solve the mystery by correcting it to hexadecimal. Incidentally, the mysterious value often comes from a character code. Therefore, it is useful to know in advance what values ​​the ASCII code table and Kanji character code are. Aside from the ASCII code, there is not one kanji code, and there are many types of kanji, so it is not easy. I think it's good.

  • Answer # 2

    &a value ~
    Is the value of b

    And then,

      

    printf ("c value is% d \ n", * c);

    This is a mistake because the value of * c is not an int

  • Answer # 3

      

    The value of c is 6422312

    c contains the address of b. (c =&b;)
    b contains the address of a. (b =&a;)
    As a result, * c is the address of a.

    So

      

    printf ("c value is% d \ n", * c);

    The value output by

    is the address of a (decimal notation).

    In C language, the address is assigned to int as it is, so be careful. Isn't it a warning for recent compilers?