Pointers in C++ programming



Definition of Pointers in C++

Every variable is assigned a memory location whose address can be retrieved using the address operator &. The address of a memory location is called a pointer.

Every variable in an executing program is allocated a section of memory large enough to hold a value of that variable’s type.

Current C++ compilers that run on PCs usually allocate a single byte to variables of type char, two bytes to variables of type short, four bytes to variables of type float and long, and 8 bytes to variables of type double.

C++ has an address operator & that can be used to retrieve the address of any variable. To use it, place it before the variable whose address you want.

Here is an expression that returns the address of the variable amount: &amount And here is a statement that displays the variable’s address to the screen:


    cout << long(&amount);

By default, C++ prints addresses in hexadecimal. Here we have used a function-style cast to long to make the address print in the usual decimal format. This program uses the & operator to determine a variable's address.


    #include <iostream>
     using namespace std;
     char letter;
     short number;
     float amount;
     double profit;
     char ch;

    int main() {
    cout << "Address of letter is: "
         << long(&letter) << endl;
    cout << "Address of number is: "
         << long(&number) << endl;
    cout << "Address of amount is: "
         << long(&amount) << endl;
    cout << "Address of profit is: "
         << long(&profit) << endl;
    cout << "Address of ch is: "
         << long(&ch) << endl;
    return 0;
    }

    Program Output :
    Address of letter is: 4468752
    Address of number is: 4468754
    Address of amount is: 4468756
    Address of profit is: 4468760
    Address of ch is: 4468768

The value &amount specifies the location of the variable amount in the computer’s memory: in a sense, it points to amount. A value that represents the address of a memory location, or holds the address of some variable, is called a pointer.

Pointer variables

A pointer variable is a variable that holds addresses of memory locations. Like other data values, memory addresses, or pointer values, can be stored in variables of the appropriate type.

A variable that stores an address is called a pointer variable, but is often simply referred to as just a pointer. The definition of a pointer variable, say ptr, must specify the type of data that ptr will point to.

Here is an example:


    int *ptr;

The asterisk before the variable name indicates that ptr is a pointer variable, and the int data type indicates that ptr can only be used to point to, or hold addresses of, integer variables.

Some programmers prefer to declare pointers with the asterisk next to the type name, rather than the variable name.

For example, the declaration shown above could be written as: int* ptr;


    #include <iostream>
    using namespace std;
    int main()
    {
    int x = 25; // int variable
    int *ptr; // Pointer variable, can point to an int
    ptr = &x; // Store the address of x in ptr
    cout << "The value in x is " << x << endl;
    cout << "The address of x is " << ptr << endl;
    return 0;
    }

    Program Output :
    The value in x is 25
    The address of x is 0x7e00

In the program above two variables are defined: x and ptr. The variable x is an int, while ptr is a pointer to an int. The variable x is initialized with 25, while ptr is assigned the address of x.


Ads Right