cplusplus:pointers_1_slides
Pointers 1 slides
Pointer fundamentals, syntax, and operations.1)
Mithat Konar
October 19, 2021
Introduction
- Pointers found in many programming languages.
- Important in C++ because a close relationship between pointers and arrays, C strings, and references.
Pointer variables
- Variables are stored in blocks of computer memory.
- base address: the memory address of the first byte of this block.
- pointer variable: a variable that stores the base address of some other variable.
- Usefulness will be seen after understanding how to use them.
Two ways to think about pointers
- Pointers hardware model
- Pointer visual model
Pointer hardware model
- Assume an
int
namedcount
storing the value 7 andint
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 |
- The address of
count
is the base address 58000.
Pointer hardware model
- A pointer variable set to point to
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 |
- The value of a pointer variable is the base address of another variable.
Pointer visual model
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
).
Pointer visual model
Changing the value of countPtr
to point to num
:
Pointer syntax basics
Pointer declaration
*
character is used to indicate pointer variables in declarations and function parameter lists.- Type of data being pointed to must also be indicated.
int *myPtr; // declare a pointer to an int bool *yourPtr; // declare a pointer to a bool
- Location of the
*
character is flexible:
int *myPtr; int* myPtr; int * myPtr;
Pointer declaration
- You can declare more than one pointer variable at a time:
int *myPtr, *anotherOne; // declare two pointers to an int
- But be careful::
int* myPtr, anotherOne; // a pointer and an int
Address operator
- To set the value of a pointer, you need the address of something.
- address operator
&
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
- Most environments show base addresses as hexadecimal numbers.
Pointer assignment
- Declare an integer variable
y
and a pointer variablemyPtr
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
Pointer assignment
- Declare an integer variable
y
and a pointer variablemyPtr
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 |
Pointer assignment
- Changing the value of pointer variables:
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
Pinter initialization
- Local pointer variables in C++ are not automatically initialized.
- Uninitialized pointers point to arbitrary memory. Very dangerous.
- You can initialize when declared:
int y = 5; int *myPtr = &y; // myPtr gets address of y
nullptr/NULL pointers
- You can set a pointer to a value that indicates that it is pointing to nothing:
nullptr
(C++11) orNULL
(earlier versions). - Otherwise uninitialized pointers should be set to
nullptr
(orNULL
).
int *yourPtr = nullptr; // yourPtr points to nothing int y = 5; yourPtr = &y; // yourPtr gets address of y
nullptr
andNULL
have an integer value of0
, so sometimes programmers use0
or'\0
'.
Operators for pointers
- address operator
&
(already covered). - indirection or dereferencing operator
*
(covered next).
Indirection/dereferencing operator
- indirection or dereferencing operator,
*
, 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)
Indirection/dereferencing operator
- Dereferencing can be used to assign a value to a location in memory:
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
- You can think of the indirection/dereferencing operator as meaning, “the_thing_at_the_end_of_”, as in:
the_thing_at_the_end_of_myPtr = 7;
Indirection/dereferencing operator
*
and&
operators complement each other.- The following expressions all evaluate as true:
*&y == y &*myPtr == myPtr *&myPtr == myPtr
Example
- pointer-example.cpp
/** 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; }
1)
Portions loosely adapted from:
Deitel, Harvey M., and Paul J. Deitel. “Pointers and Strings.” In C++: How to Program. 3 ed. Upper Saddle River, NJ: Prentice Hall, 2001. 304-388.
cplusplus/pointers_1_slides.txt · Last modified: 2021/10/19 19:29 by mithat