Define Pointer
1 | int* ptr; |
This declares ptr
as a pointer to int
Operators
*
Operator- Means “Pointer” when assignment and initialization.
- Means “Go To” the address it stored.
&
Operator
Means “Get Address”.
Usage:
- Initialization
1 | int i=10; |
- Assignment
1 | int i = 10; |
List Pointer
Let’s take this as an example.
Notice that type of tha array must match that of the pointer.
1 | int a[10]; |
Assign the address of a[0]
to pointer ptr
.
1 | ptr = &a[0]; |
In C, this could be replaced by:
1 | ptr = a; |
Also, because elements in an array are continuous:
1 | *(ptr+i) == a[i] == *(a+1) // True |
Remind that a
could not change, but ptr
could change.