Take this piece of code:
int a;
int *pointer = &a;
int **b = &(&(*pointer));
Would the above set b
to the address of pointer
or not?
The reason I ask is because *pointer
gives the value of a
, and the reference of that is the address of a
. Is this treated as just the address of a
, or is it also treated as pointer
.
Does this make sense? Could I do:
&(*pointer) = a;
The unary address-of operator ( & ) returns the address of (that is, a pointer to) its operand. The operand of the address-of operator can be a function designator or an lvalue that refers to an object that's not a bit field.
As just seen, a variable which stores the address of another variable is called a pointer. Pointers are said to "point to" the variable whose address they store. An interesting property of pointers is that they can be used to access the variable they point to directly.
A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.
Pointers: Pointing to the Same AddressThere is no limit on the number of pointers that can hold (and therefore point to) the same address.
No. In C, you can only get a pointer to a storage area (which means a variable, an array element, or another pointer; they call those "l-values"), not to any expression. You cannot get a pointer to an expressions that has no defined storage area (like an addition, or the result of a function call). It should be noted however that C++ messes these rules with references, but for the sake of clarity, I'll leave it out.
Pointers aren't magical: in the end, they're just integers. Therefore, when you get the pointer of a pointer, it's just like you were getting the pointer of an integer. It has no more repercussions.
For instance, if you get the pointer to a
in your code, you're just copying this address in another variable. Nothing keeps you from changing said variable:
int a;
int* p = &a;
p = NULL;
And doing this, you a
will remain unaltered. All you can change about a
is its value. Its address is immutable. Anything else would imply that &a = NULL
(or any other pointer value) would work, which doesn't.
int **b = &(&(*pointer));
This doesn't, or shouldn't compile.
You can only take the address of an l-value. (See below for a description)
C++03 S5.3.1-2:
The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid. In the first case, if the type of the expression is “T,” the type of the result is “pointer to T.” In particular, the address of an object of type “cv T” is “pointer to cv T,” with the same cv-qualifiers. For a qualified-id, if the member is a static member of type “T”, the type of the result is plain “pointer to T.” If the member is a nonstatic member of class C of type T, the type of the result is “pointer to member of class C of type
...and the reference of that is the address of a...
Also you are using the term reference wrong here. & is a symbol that is used for different things. one of those things is to declare references, an unrelated thing is the address of unary operator. The later is not called a reference.
Does this make sense? Could I do:
&(*pointer) = a;
An address of a variable, and hence &(*pointer)
or equivalently &a
are r-values.
You can't assign anything to an r-avlue. Ignoring things like const you can consider an r-value something that must appear on the right hand side. An l-value is kind of like left hand side but really it means it can be stored in a storage location (the difference is because a const object for example can't appear on the left hand side but it is still considered an l-value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With