Pointers:
- A pointer is a variable whose value is the address of another
variable.
- It can be used to perform pass-by-reference.
- It can be used to create and manipulate dynamic data
structures
Pointer Variable Declarations and
Initialization:
Pointer variables
- Contain memory addresses as values.
- Normally, variable contains specific value (direct
reference)
- Pointers contain address of variable that has specific value
(indirect reference)
Pointer declarations
- * indicates variable is a pointer
- Example : int *myPtr;
- Declares pointer to int, of type int *
- • Multiple pointers require multiple asterisks int *myPtr1,
*myPtr2;
Pointer initialization:
- Initialized to 0, NULL, or an address.
- 0 or NULL points to nothing (null pointer).
Pointer Operator:
- Address operator (&)
- Returns memory address of its operand
- Example
- int y = 5;
- int *yPtr;
- yPtr = &y;
- assigns the address of variable y to pointer
- variable yPtr – Variable yPtr “points to” y
- yPtr indirectly references variable y’s value
- * operator
- Also called indirection operator or dereferencing operator
- Returns synonym for the object its operand points to
- *yPtr returns y (because yPtr points to y)
- Dereferenced pointer is an lvalue
- * and & are inverses of each other
- Will “cancel one another out” when applied consecutively in
either order
Pass-by-Reference with Pointers:
- Simulates pass-by-reference
- Use pointers and indirection operator
- Pass address of argument using & operator
- Arrays not passed with & because array name is already a
pointer
- * operator used as alias/nickname for variable inside of
function
Using const with Pointers:
- const qualifier
- Indicates that value of variable should not be modified
- const used when function does not need to change the variable’s
value
sizeof Operator:
- Returns size of operand in bytes
- For arrays, sizeof returns
- ( size of 1 element )
- * ( number of elements )
- If sizeof( int ) returns 4 then
- int myArray[ 10 ];
- cout << sizeof( myArray );
- will print 40
- Can be used with
- Variable names
- Type names
- Constant values
Pointer Expressions and Pointer Arithmetic:
- Pointer arithmetic
- Increment/decrement pointer (++ or --)
- Add/subtract an integer to/from a pointer (+ or +=, - or
-=)
- Pointers may be subtracted from each other
- Pointer arithmetic is meaningless unless performed on a pointer
to an array
- 5 element int array on a machine using 4 byte ints
- vPtr points to first element v[ 0 ], at location 3000 vPtr =
&v[ 0 ];
- vPtr += 2; sets vPtr to 3008 (3000 + 2 * 4)
- Subtracting pointers
- Returns number of elements between two addresses vPtr2 = v[ 2
];
- vPtr = v[ 0 ];
- vPtr2 - vPtr is 2
- Pointer assignment
- Pointer can be assigned to another pointer if both are of same
type
- If not same type, cast operator must be used
- Exception
- Pointer to void (type void *)
- Generic pointer, represents any type
- No casting needed to convert pointer to void *
- Casting is needed to convert void * to any other type
- void pointers cannot be dereferenced
- Pointer comparison
- Use equality and relational operators
- Compare addresses stored in pointers
- Comparisons are meaningless unless pointers point to members of
the same array
- Example
- Could show that one pointer points to higher-index element of
array than another pointer
- Commonly used to determine whether pointer is 0 (null
pointer)
Relationship Between Pointers and Built-In
Arrays:
- Arrays and pointers are closely related
- Array name is like constant pointer
- Pointers can do array subscripting operations
- Accessing array elements with pointers
- Assume declarations:
- int b[ 5 ];
- int *bPtr;
- bPtr = b;
- Element b[ n ] can be accessed by *( bPtr + n )
- Called pointer/offset notation
- Addresses
- &b[ 3 ] is same as bPtr + 3
- Array name can be treated as pointer
- b[ 3 ] is same as *( b + 3 )
- Pointers can be subscripted (pointer/subscript notation)
- bPtr[ 3 ] is same as b[ 3 ]
Pointer-Based String:
- String
- Series of characters treated as single unit
- Can include letters, digits, special characters +, -, *,
...
- String literal (string constants)
- Enclosed in double quotes, for example:
- Have static storage class
- Array of characters, ends with null character '\0'
- String is constant pointer
- Pointer to string’s first character
- String assignment
- Character array
- • char color[] = "blue";
- Creates 5 element char array color
- Variable of type char *
- char *colorPtr = "blue";
- Creates pointer colorPtr to letter b in string "blue"
- "blue" somewhere in memory
- Alternative for character array
- char color[] = { 'b', 'l', 'u', 'e', '\0' };
- Reading strings
- Assign input to character array word[ 20 ]
- cin >> word;
- Reads characters until whitespace or EOF
- String could exceed array size
- cin >> setw( 20 ) >> word;
- Reads only up to 19 characters (space reserved for '\0')
- cin.getline
- Read line of text
- cin.getline( array, size, delimiter );
- Copies input into specified array until either
- One less than size is reached
- delimiter character is input
- Example
- char sentence[ 80 ];
- cin.getline( sentence, 80, '\n' );
Smart Pointers: