The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like:
int a = 5;
int *b = &a;
Now my question is, why does this type of code,
int a = 5; int *b = &a; int *c = &b;
generate a warning?
You need to go back to the fundamentals.
p
is a pointer value then *p
is a variablev
is a variable then &v
is a pointerAnd now we can find all the mistakes in your posting.
Then assume that now I want to save the address of pointer
*b
No. *b
is a variable of type int. It is not a pointer. b
is a variable whose value is a pointer. *b
is a variable whose value is an integer.
**c
refers to the address of*b
.
NO NO NO. Absolutely not. You have to understand this correctly if you are going to understand pointers.
*b
is a variable; it is an alias for the variable a
. The address of variable a
is the value of variable b
. **c
does not refer to the address of a
. Rather, it is a variable that is an alias for variable a
. (And so is *b
.)
The correct statement is: the value of variable c
is the address of b
. Or, equivalently: the value of c
is a pointer that refers to b
.
How do we know this? Go back to the fundamentals. You said that c = &b
. So what is the value of c
? A pointer. To what? b
.
Make sure you fully understand the fundamental rules.
Now that you hopefully understand the correct relationship between variables and pointers, you should be able to answer your question about why your code gives an error.