Pointers in C++

A pointer is a variable that holds a memory address. It is called a pointer because it points to the value at the address that it stores.

Pointers in C++

If you want to declare a pointer variable in c++ you must first choose what data type it will point to such as an int or a char. You then declare it as if you were declaring a variable in the normal way and then put a * in front of its name to show that it is a pointer. Here is an example of how to declare a pointer to an integer.

int *pi;

You can store the address of another variable in a pointer using the & operator. See the below example of how to store the address of a variable called i in the pointer called pi.

int i;
int *pi;
pi = &i;

You must dereference a pointer to get the value at the memory location that the pointer points to. You use the * operator to dereference a pointer. Here is an example of how we first set the value of i to 5 and then set its value to 7 by dereferencing the pointer.

int i = 5;
int *pi;
pi = &i;
*pi = 7;
cout << i;

new & delete operators

The new operator is used to allocate memory that is the size of a certain data type. It returns a pointer to the address of the newly allocated memory. Here is an example of how to allocate memory for an integer and then set its value to 5.

int *pi;
pi = new int;
*pi = 5;

The delete operator deallocates memory. You need to deallocate the memory for all the memory that you have previously allocated before exiting the program or else you will have memory leaks.

int *pi;
pi = new int;
*pi = 5;
delete pi;

Typed and untyped pointers

We have been using typed pointers so far because they point to a specific data type. An untyped pointer can point to anything. You declare an untyped pointer as the data type void.
void *up;

malloc() and free() functions

The malloc command allocates a certain number of bytes and returns a pointer to the first byte. You must use the free command to deallocate the memory that was allocated with malloc. To be able to use malloc and free you must include the malloc header file. Here is an example that allocates 100 bytes of memory and stores the address of it in the pointer called up and then deallocates the memory.

void *up;
up = malloc(100);
free(up);

Pointers in C

A pointer is a variable suitable for keeping memory addresses of other variables. The values you assign to a pointer are memory addresses of other variables or other pointers.

Pointers in C are characterized by their value and data-type. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function.

Pointers are declared by using the asterisk(*).
int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed but the value may change.

void main (void)
{

int i;
int * a;
i = 10;
a = &i;
printf (” The address of i is %8u \n”, a);
printf (” The value of i is %d\n”, i);
printf (” The value at that location is %d\n”, *a);

}

Output

The address of i is 631672
The value of i is 10
The value at that location is 10

Arrays and Pointers

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

Array
Pointer
arr[0] *arr
arr[1] *(arr+1)
arr[n] *(arr+n)