C++ Pointers


A pointer is used in the C++ program to access the memory and manipulate the address which is defined. Pointers are variables in the C++ program which stores the address of another variable.

Syntax:

data_type *pointer_name; 
c++ pointer

Here, * is used to denote that “pointer_name” is pointer variable and it is not the normal variable.


Advantage Of Pointer:-

There are major advantages of pointers using in C++ program:

  • Allows management of structures which are allocated memory dynamically.
  • Allows passing of arrays and strings to functions more efficiently.
  • Possible to pass the address of the structure instead of the whole structure of the functions.
  • Possible to return more than one value from the function.

Example:-

#include < iostream> 
using namespace std;

int main() 
{   
    int a=10;   
    int *p;      
    p=&a; /* store address of a variable in pointer variable*/
    cout << "Address of a variable is " << &a << endl;
    cout << "Address of p variable is " << p << endl;
    /* access the value using the pointer */
    cout << "Value of *p variable is  " << *p << endl;
    return 0;
}

Usage of pointers - Dynamic memory allocation

Pointers are used in three different ways:

  • It is used to create dynamic data structures.
  • It is used to pass and control variable parameters passed to functions.
  • It is used to access information stored in arrays.

Address of the operator:

Address operator is denoted by ‘&’ symbol. When we use this symbol as a prefix to a variable name, it gives the address of the variable.

&x - It gives an address of the x variable.


Declaring a pointer:

Syntax:

data_type *pointer_name;

Data_type

  • This is the type of variable that the pointer points to
  • A data type whose address is stored in pointer name

Asterisk( * )

  • It is called as an indirection operator
  • It is also called as the value at address operator
  • It indicates variable declared as a pointer type

Pointer_name

  • It must be any valid C++ identifier
  • It must follow all rules of variable name declaration

Null Pointer:

A pointer that is assigned nothing is called the null pointer. The null pointer points out the base address of the segment. A pointer that initialized with the null value is known as the null pointer. In case, if you don’t have an address to assign a pointer then you can use null.

Example:-

#include    
using namespace std;

int main() 
{   
    int *p=NULL;      
    cout << "The value of p is " << p << endl;
    return 0;
}

Defining null value

#define NULL 0




OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.