Pointer. Very basic question...

Asked 2 years ago, Updated 2 years ago, 42 views

#include <stdio.h>

int main()
{
    int a = 10;
    int *pa = &a;
    *pa = 20;
    printf("%d", a);
    return 0;
}

In this code, the 10 assigned to the memory digit of a is changed to 20 Is it right to change it to 20 when printing it with printf?

I'm sorry to ask you such a basic question.

pointer

2022-09-21 19:03

1 Answers

Yes, that's right.

I tested it with a c++ interpreter called cling.

Look carefully at the memory address.

Also, the part that can be confused when expressing the pointer is the int *pa=&a mark, which is better to read by attaching * to the data type in the form of int *pa=&a. In other words, it reads naturally, "The variable pa is the int pointer type."

[cling]$ int a = 10
(int) 10
[cling]$ &a
(int *) 0x7fa318174010
[cling]$ int *pa = &a
(int *) 0x7fa318174010
[cling]$ *pa = 20
(int) 20
[cling]$ pa
(int *) 0x7fa318174010
[cling]$ a
(int) 20


2022-09-21 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.