Pointer fundamentals, syntax, and operations.1)
Mithat Konar
October 19, 2021
int
named count
storing the value 7 and int
s occupy 4 bytes.Variable name | Memory location | Memory contents across all four bytes (32 bits) |
---|---|---|
count | 58000 | 00000000000000000000000000000111 (i.e. 7 in decimal) |
58001 | ||
58002 | ||
58003 |
count
is the base address 58000. count
would store the value 58000.Variable name | Memory location | Memory contents across all 8 bytes (64 bits) |
---|---|---|
countPtr | 64002 | 00000000000000000000000000000000 (58000 in decimal) |
64003 | ||
… | ||
64009 |
Given the count
variable used above:
a pointer variable countPtr
pointing to count
would be represented as:
The value of countPtr
is the box the arrow points to (i.e., the integer count
).
Changing the value of countPtr
to point to num
:
*
character is used to indicate pointer variables in declarations and function parameter lists.int *myPtr; // declare a pointer to an int bool *yourPtr; // declare a pointer to a bool
*
character is flexible:int *myPtr; int* myPtr; int * myPtr;
int *myPtr, *anotherOne; // declare two pointers to an int
int* myPtr, anotherOne; // a pointer and an int
&
returns the base address of a variable:int num = 42; cout << num << endl; // prints value held in variable num cout << &num << endl ; // prints the base address of variable num
y
and a pointer variable myPtr
set to store the address of (“point to”) y
.int y = 5; // declare an integer variable y int *myPtr; // declare a pointer to int myPtr = &y; // myPtr gets address of ("points to") y
y
and a pointer variable myPtr
set to store the address of (“point to”) y
.int y = 5; // declare an integer variable y int *myPtr; // declare a pointer to int myPtr = &y; // myPtr gets address of ("points to") y
Variable name | Memory location | Memory contents |
---|---|---|
y | 52000 | 5 |
… | ||
52003 | ||
… | … | … |
myPtr | 63002 | 52000 |
… | ||
63009 |
double z = 3.33; double x = 42.0; double *myPtr; myPtr = &z; // myPtr gets address of z myPtr = &x; // myPtr now has address of x
int y = 5; int *myPtr = &y; // myPtr gets address of y
nullptr
(C++11) or NULL
(earlier versions).nullptr
(or NULL
).int *yourPtr = nullptr; // yourPtr points to nothing int y = 5; yourPtr = &y; // yourPtr gets address of y
nullptr
and NULL
have an integer value of 0
, so sometimes programmers use 0
or '\0
'.&
(already covered).*
(covered next).*
, accesses the value of what its operand points to.int y = -1; // declare y and initialize its value int *myPtr = &y; // declare pointer and set it to point to y cout << *myPtr; // defererence myPtr (prints -1)
int y = -1; // declare y and initialize its value int *myPtr = &y; // declare pointer and set it to point to y *myPtr = 7; // change value in y to 7 cout << y; // prints 7
the_thing_at_the_end_of_myPtr = 7;
*
and &
operators complement each other.*&y == y &*myPtr == myPtr *&myPtr == myPtr
/** Demonstrate basic pointer usage. */ #include <iostream> using namespace std; int main() { int a; // a is an integer int *aPtr = nullptr; // aPtr is a pointer to an integer a = 7; // give a a value aPtr = &a; // set aPtr to the address of a cout << "The value of a is: " << a << endl << "The address of a is: " << &a << endl << "The value of aPtr is: " << aPtr << endl; cout << endl; cout << "The value of a is: " << a << endl << "The value of *aPtr is: " << *aPtr << endl; cout << endl; cout << "Showing that * and & are inverses of each other:" << endl << "&*aPtr = " << &*aPtr << endl << "*&aPtr = " << *&aPtr << endl; return 0; }