Introducing pointers

Illustrate the concept of pointers in the C++ programming language.
Pointers represent an important aspect of C++. Although, newbies often get confused with pointers, even those who know other programming languages.

As a C++ newbie, it’s important for you to understand pointers.

Pointer = computing computer memory address: an address, stored as data in a computer’s memory, that is the location at which the desired data is stored
Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.

Every variable and function has its space in the memory where it is stored.
Considering you already know the C++ data types (if not check this tutorial), every type of variable occupies a different space.
I hope you agree that a ‘short’ variable uses less space than an ‘int’ variable (although, on some systems it may be equal).

Getting the address where a variable is stored
We get the address of a variable by using a ‘&’ prefix on the variable.

It’s a matter of where and when: When we use this we won’t get the value stored by the variable, we get the address of that variable, where it actually stores the data.

This example should give you a clear image of how to get the address of a variable.

// Introducing pointers

#include <iostream>
using namespace std;

int x = 12;
double y = 8.4;
int z = 4;

int main()
{
    cout << "Value of 'x' is: " << x << "\n";
    cout << "Address of 'x' is: " << &x << "\n\n";

    cout << "Value of 'y' is: " << y << "\n";
    cout << "Address of 'y' is: " << &y << "\n\n";

    cout << "Value of 'z' is: " << z << "\n";
    cout << "Address of 'z' is: " << &z << "\n\n";
    return 0;
}

Now you ask: what am I supposed to do with the address?
This you will find out at the end. We are not yet finished… actually we didn’t even created a pointer yet.

Creating a pointer

A pointer is a type of variable, special for holding the address of some other variable.
In the earlier example we retrieved the address of the ‘x’ variable by using ‘&x;’. Now we can store it in this special type of variable, the pointer.

As we said earlier, depending on the type, a variable can occupy more or less space in the memory. This is why when we create a pointer to a variable, we must specify the type of variable it actually points to.
Let’s see an example:

int x = 12;
int * pntr = &x;

In the first line we declare a variable ‘x’ that holds the value ’12’.
In the second line int * pntr means “pntr points to a variable of int type/size”, because ‘x’ is an ‘int’ type of variable.
We actually assign the address to the pointer using ‘= &x;’. We know that ‘&x;’ is the address of the variable ‘x’ and the rest of the assignment is done exactly like in an usually variable… using the ‘=’ sign.

Now ‘pntr’ holds the address of the ‘x’ pointer, ‘00476748’ for example.

The asterisk (*) between ‘int’ and ‘pntr’ isn’t confused with the multiplication operator because of the context in which it is used.

Now it’s time to say we can access the content of the ‘x’ variable using ‘pntr’.
“How do we do that because ‘pntr’ stores the address, and not the content of the ‘x’ variable”, you cry?
Answer: By using ‘*pntr’.
Try it:

// Accessing a variable through a pointer

#include <iostream>
using namespace std;

int x = 12;
int * pntr = &x;

int main()
{
    cout << "x = " << x << "\n";
    cout << "&x = " << &x << "\n\n";
    cout << "pntr = " << pntr << "\n";
    cout << "*pntr = " << *pntr << "\n\n";
    return 0;
}

‘*pntr’ is called ‘accessing a variable through a pointer’… that’s because we retrieve the value of ‘x’ by using its pointer.

What’s so special about the pointer?

Here I’ll answer your two big question about pointers:
“Still, what am I supposed to do with the address?”
and
“Why can’t I use an usual (‘int’ perhaps) variable to hold the address?”

Probably you already know that a variable inside a function is different from the variable outside of that function.
If you don’t already know that, check the following example:

// Variables inside and outside functions

#include <iostream>
using namespace std;

void funct(int x)
{
    cout << "(Inside) x is: " << x << "\n";
    x = 6;
    cout << "(Inside) x is changed to: " << x << "\n";
}

int main()
{
    int x = 25;
    funct(x);
    cout << "(Outside) x remains: " << x << "\n";
    return 0;
}

Is there any possible way to change the value of the variable? Here is where pointers become useful.
We know that in the earlier examples, ‘pntr’ had the address and ‘pntr’ the value of the pointed variable. By changing ‘pntr’ we also change ‘x’.

// Using pointers with functions

#include <iostream>
using namespace std;

void funct(int& pntr)
{
    pntr = 12;
}

int main()
{
    int x = 5;
    cout << "Before the function, x is: " << x << "\n";
    funct(x);
    cout << "After the function, x is: " << x << "\n";
    return 0;
}

We answered both questions:
Q: Still, what am I supposed to do with the address?
A: Storing it in a pointer and using it for changing values inside functions, but no just that. Later in C++ you will see how pointers continue to be helpful.

Q: Why can’t I use an usual (‘int’ perhaps) variable to hold the address?
A: Because when you change the value of the second variable, the original variable will remain the same…
If you have ‘x = 5’ and assign ‘y = x’, you will then have ‘x = 5’ and ‘y = 5’. But if you do ‘y = 12’, x will still be 5.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top