Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Chapter – Three

C++ Pointers
Pointers are symbolic representations of addresses. They enable programs to simulate
call-by-reference as well as to create and manipulate dynamic data structures. Iterating
over elements in arrays or other data structures is one of the main use of pointers. The
address of the variable you’re working with is assigned to the pointer variable that
points to the same data type (such as an int or string).
Syntax:
datatype *var_name;
int *ptr; // ptr can point to an address which holds int data

How to use a pointer?

 Define a pointer variable


 Assigning the address of a variable to a pointer using the unary operator (&) which
returns the address of that variable.
 Accessing the value stored in the address using unary operator (*) which returns the
value of the variable located at the address specified by its operand.
The reason we associate data type with a pointer is that it knows how many bytes the
data is stored in. When we increment a pointer, we increase the pointer by the size of
the data type to which it points.

Pointer example

Output
Value at ptr = 0x7ffe454c08cc
Value at age = 20
Value at *ptr = 20
Array Name as Pointers

An array name contains the address of the first element of the array which acts like a constant
pointer. It means, the address stored in the array name can’t be changed. For example, if we have
an array named val then val and &val[0] can be used interchangeably. 
Pointer Expressions and Pointer Arithmetic
As you understood pointer is an address which is a numeric value; therefore, you can perform
arithmetic operations on a pointer just as you can a numeric value. There are four arithmetic
operators that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the
address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the
pointer –.

The ptr will point to the location 1004 because each time ptr is incremented, it will point to the
next integer. This operation will move the pointer to next memory location without impacting
actual value at the memory location. If ptr points to a character whose address is 1000, then
above operation will point to the location 1001 because next character will be available at 1001.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the variable pointer can be
incremented, unlike the array name which cannot be incremented because it is a constant pointer.
The following program increments the variable pointer to access each succeeding element of the
array −.
When the above code is compiled and executed, it produces result something as follows

Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its value by the
number of bytes of its data type as shown below −.

When the above code is compiled and executed, it produces result something as follows –
Pointers and String literals
String literals are arrays containing null-terminated character sequences. String literals are
arrays of type character plus terminating null-character, with each of the elements being of
type const char (as characters of string can’t be modified).

This declares an array with the literal representation for “geek”, and then a pointer to its
first element is assigned to ptr. If we imagine that “geek” is stored at the memory
locations that start at address 1800, we can represent the previous declaration as: 

Pointers to pointers
In C++, we can create a pointer to a pointer that in turn may point to data or another
pointer. The syntax simply requires the unary operator (*) for each level of indirection
while declaring the pointer.

char a;
char *b;
char ** c;
a = ’g’;
b = &a;
c = &b;
Here b points to a char that stores ‘g’ and c points to the pointer b.
Void Pointers
This is a special type of pointer available in C++ which represents the absence of type.  Void
pointers are pointers that point to a value that has no type (and thus also an undetermined
length and undetermined dereferencing properties). This means that void pointers have great
flexibility as they can point to any data type. There is a payoff for this flexibility. These
pointers cannot be directly dereferenced. They have to be first transformed into some other
pointer type that points to a concrete data type before being dereferenced.  
Invalid pointers
A pointer should point to a valid address but not necessarily to valid elements (like for
arrays). These are called invalid pointers. Uninitialized pointers are also invalid
pointers.
int *ptr1;
int arr[10];
int *ptr2 = arr+20;
Here, ptr1 is uninitialized so it becomes an invalid pointer and ptr2 is out of bounds of
arr so it also becomes an invalid pointer. (Note: invalid pointers do not necessarily raise
compile errors)
NULL Pointers
A null pointer is a pointer that point nowhere and not just an invalid address. Following
are 2 methods to assign a pointer as NULL;
int *ptr1 = 0;
int *ptr2 = NULL;
C++ Pointers vs Arrays

Advantages of Pointers
 Pointers reduce the code and improve performance. They are used to retrieve strings, trees,
arrays, structures, and functions.
 Pointers allow us to return multiple values from functions.
 In addition to this, pointers allow us to access a memory location in the computer’s
memory.

You might also like