January 13, 2021

C++ Pointers

In this lesson, we would learn about C++ pointers. This is one of the few topics that is distinguishes C++ from many other programming languages. So, it’s important you understand it.

  1. Overview of C++ Pointers
  2. Creating Pointers
  3. Using Pointers in C++
  4. Some Pointer Concepts

 

1. Overview of C++ Pointers

Pointers in C++ are variables that store memory address of other variables. So if you have a variable x, then to get its memory address, you use &x. The code below displays address of three variables

// Printing memory addresses of variables
int main () {
    // declare some variables
    int x = 67;
    string name = "Kindson";
    double y = 54.32;

    // print address of x
    cout << "Address of x: "<< &x << endl;

    // print address of name
    cout << "Address of name: " << &name << endl;

    // print address of y
    cout << "Address of y: " << &y << endl;

    return 0;
}

The output of the program is:

Address of x: 0x62fe08
Address of name: 0x62fde0
Address of y: 0x62fdd8

As you can see, the addressed are shown in hexadecimal (begins with 0x)

 

2. Creating Pointers

Similar to normal variables, you also need to declare a pointer before you can use it. However, pointer names are preceded by an asterisk(*). For example

int *myPointer; //pointer to int

int* myPointer; //pointer to int

int* myPointer, myVar; //a pointer and an int

The three lines above are all valid ways to declare a pointer. The first and second lines declares a pointer myPointer which is a pointer to an int variable. Third line declares a pointer myPointer and a normal variable myVar.

 

3. Using Pointers in C++

Let’s now create a pointer and assign the address of a variable to it.

// Working with point
int main () {
    // declare a variables, x
	int x = 34;

	// declare a pointer px
	int *px;

	// assign the pointer a value
	px = &x;

	// display the pointer value
	cout<<"Variable x " << x << " is stored at " << px;

    return 0;
}

The output is:

Variable x 34 is stored at 0x62fe14

Moreover, if you have a pointer and you want to know what value is stored there, you just also use *. The code below would get the value stored in px.

cout<<"Value stored in " << px << " is " << *px;

 

4. Some Pointer Concepts

There are a few concepts on pointers I would like to mention here finally.

SNConcept & Description
1Null PointersA null pointer is is a constant with a value of zero defined in several standard libraries.
2Pointer ArithmeticAlso, you can also perform arithmetic operations with pointers. The four arithmetic operators that you can use on pointers are: ++, –, +, –
3Pointers vs ArraysThere is a relationship between pointers and arrays. We’ll explore this in later lessons
4Array PointersYou can define arrays to hold a number of pointers.
5Pointer to PointerC++ allows you to have pointer holding the memory address of another pointer.
6Passing Pointers to FunctionsPassing arguments by reference or by address both allow for the passed argument to be modified in the calling function by the called function.
7Returning Pointer from FunctionsYou can also allows to function to return a pointer to local variable, static variable as well asĀ  dynamically allocated memory.
I recommend you ensure to understand clearly the concept of pointers in C++. Get more support from my YouTube Channel, Kindson The Tech Pro.

One thought on “C++ Pointers

Leave a Reply

Your email address will not be published. Required fields are marked *