What Are The Pointers in C ++ with Examples

What are Pointers?

In C++, a pointer is a variable that can store the address of another variable. Like normal variables, pointers have a data type. For example, a pointer of a type integer can store the address of an integer variable. A float-type pointer can store the address of a float-type variable. A character-type pointer can store the address of a character-type variable.

Addresses in C++

To understand the pointers, you must have a need to understand how a computer stores data.

When you assign a variable in your program, it occupied some space in the computer memory, like integer type takes 4 bytes, float takes 4 bytes and char takes 1 byte of memory. A pointer variable is used to store the address of a variable.

To know the location in the computer memory where the data is stored, C++ provides the & (address to) operator. The operator gives the address where the variable is stored.

Pointer Declaration Syntax

The syntax of pointers:

datatype *variable_name;

  • The datatype means which type of variable you write example integer, float, and character.

An example of pointer declarations in C++:

int *ptr; // a pointer to integer

double *ptr; // a pointer to double

float *ptr; // a pointer to float

char *ptr; // a pointer to a character

Reference (&) and Deference operator (*)

The variable’s address is returned by the reference operator.

We can find the value that was stored in a memory address with the help of the deference operator.

For example:

Suppose we have a variable whose name is xyz, stored in the address 84432 and storing the value 28.

The reference operator (&) will return 84432 means the address of a variable.

The dereference operator (*) will return 5 mean the value of a variable.

Example 1:

#include <iostream>int printadd(int *a);main(){int i=4;printf("address before calling function :%d\n",&i);printf("address after calling function:%u\n",printadd(&i));//printadd(&i)}int printadd(int *a){int *add;  // or  int add; both are sameadd= &(*a);return add;}

Output:

Here is a screenshot of the code:

Pointers and Arrays

A similar idea underlies how pointers and arrays function. When working with arrays that contain pointers, there are various aspects to keep in mind. The base address of the array is indicated by the array name itself. This indicates that you shouldn’t use an ampersand when giving a pointer the address of an array.

For example:

ptr = ary;

Since the array represents the array’s address, the above statement is correct. Here is another example:

ptr = &ary;

The above is incorrect.

An array can be converted into a pointer. For example:

int ary [20];int * ptr;

Below is a valid operation:

ptr = ary;

After declaration, ptr and array are equal, and they can share their properties. However, a different address can be assigned to ptr, but we cannot assign anything to arr.

NULL Pointer

Sometimes the pointer gives a NULL value if there isn’t an exact address to be assigned. It can be done during the declaration.

Example 3:

#include <iostream>using namespace std;int main() {int *ip = NULL;cout << "Value of ip is: " << ip;return 0;}

Output:

Pointers of Variables

With C++, you can manipulate data directly from the computer’s memory.

Due to pointers, it is possible to assign or reassign the memory space.

An address in the computer’s memory is referenced by a pointer variable, which points to that location.

It can be declared as follows:

int *p;

Or,

int* p;

In this example, we declared the pointer variable p.

The asterisk shows the value present at the given memory address.

Example 4:

#include <iostream>using namespace std;int main() {int *p, x = 30;p = &x;cout << "Value of x is: " << *p;return 0;}

Output:

Application of Pointers

In function, we can’t swap the value of two variables because the value of the variable is always copied from the main body and goes to the function body so we can’t change the actual values of these variables if use a pointer then we change the actual values of these variables and easily swap the values that is one of the examples of pointers.

Example 5:

#include <iostream>using namespace std;void test(int *n1, int *n2);int main() {int a = 5, b = 5;cout << "Before changing:" << endl;cout << "a = " << a << endl;cout << "b = " << b << endl;test(&a, &b);cout << "\nAfter changing" << endl;cout << "a = " << a << endl;cout << "b = " << b << endl;return 0;}void test(int *n1, int *n2) {*n1 = 10;*n2 = 11;}

Output:

Advantages of using Pointers

  • A pointer is a variable that stores the address of another variable.

Leave a Reply

Your email address will not be published.