Pointer
&
returns the memory address of the object
- called "address of" operator
- Therefore, this gets applied to the value to get the memory address
- ie.
p = &c;
*
either:
- declares a pointer variable, or
- ex.
int *p;
- ex.
- dereferences an existing pointer (indirection through a pointer)
- called "indirection" or "dereferencing" operator
- Therefore, this gets applied to the pointer to get the value in storage
int a = 5
thenint *ptr = &a;
- this means "locate" the address where
a
is stored, and assign its value toptr
- In other words, the value of
ptr
will now be the address wherea
is stored.
- In other words, the value of
- this means "locate" the address where
*ptr = 8
- this means take the address of
ptr
, "locate" that address in memory, and set its value to 8.
- this means take the address of
int *ptr
- "
ptr
is a pointer that points to an object of typeint
", or simply: "ptr
is a pointer toint
."
- "
If p points to the integer x, then *p can occur in any context where x could, so:
*p = *p + 10;
// is the same as
x = x + 10;
-
This picture shows how a pointer references a storage location, which holds a variable
c
. When we use the&
operator, we are talking about the place wherec
is stored. When we use the*
operator, we are talking about the variablec
itself. -
each pointer points to a specific data type, which is why we declare a pointer variable
int *p;
- The exception is "pointer to void", which can hold any kind of pointer, but cannot be dereferenced itself.
-
C does not implicitly initialize storage duration of memory locations. Therefore, we should be careful that the address that the pointer points to is valid.
- For this reason, some suggest initializing pointers to
NULL
(null pointer/null reference) - null pointer shown as
0x00000000
- For this reason, some suggest initializing pointers to
-
Like other languages, manipulating a function argument will have no effect on the original variable that was passed to the function. This is because when we pass an argument, a copy is made and we are merely mutating the copy.
- However, we are also able to call a function, passing in the variable's *address* as the argument `passByAddr(&x)`.
from within the function, if we change the value like so
*m = 14
. this changes the value at the address, so outside the function we'll notice that the value changed