Pointers: The Basics

Emma Gouti
Jan 21, 2019 · 4 min read

WHAT ARE THEY?

Image for post
Image for post

Pointers are one of the most powerful fundamentals of the C/C++ language, because developers are given direct access to the memory from one’s code in an easy and fast way. Pointers are variables whose value is the address of another variable, i.e., direct address of the memory location. So, in a sense pointers are a symbolic representation of addresses. They enable programs to simulate pass by reference, or pass by address, and can create and manipulate dynamic data structures. In other words, pointers allocate memory dynamically, which means memory allocated “on the fly” during run time that is usually placed in a program segment known as the heap or the free store. And when variables are passed by reference, the actual address is passed. If you’re still confused with the difference between pass by value and pass by reference, think of it like this: in the case of call by value we pass copies of values of passing arguments, in case of pass by reference, the address of the values is passed and no copies of the actual values are made. The important thing to take away is that, if you pass by reference, any manipulation of the data is reflected throughout. This means that if the value of a parameter changes in a function, that change will be reflected in the actual variable that you passed during the function call. However, the same is not true for pass by value. The changes are not reflected in the actual parameters.


HOW TO DECLARE A POINTER

To declare a pointer variable, you need to specify the data type of the value in the memory location that pointer variable points to. The syntax will look something like datatype *identifier;, with the asterisk symbol being placed anywhere between the two. For convention, the asterisk is typically attached to the variable name, like this: int *ptr or char *ch. All of the examples below, however, are equivalent.

int *ptr;    
char *ch;
int * ptr;
int* ptr;

In the example above, ptr is a pointer, and its type will be specifically be referred to as “pointer to int”, because it stores the address of an integer variable.

You can assign the address of a variable to a pointer using the unary ampersand operator (&), which returns the address of that variable. This would like this:

int a_int = 123;    // assigns a variable of type int to 123 
double num = 2.0;
int *my_ptr = &a_int; // assigns pointer to address of a_int
double *ptr_1 = #
Image for post
Image for post

To access the value stored in the address, use the unary operator (*), also known as the indirection or dereferencing operator, which returns the value of the variable located at the address specified by its operand. In other words, once a pointer is declared, you can refer to the thing or object or target it points to by “dereferencing the pointer”.

Now that ptr is declared as a pointer to an int, the variable ptr stores the address. To dereference the pointer, use the * operator:

cout << p;      // this will print out the address stored in p
cout << *p; // this will print out the data being pointed to

NOTE: The notation can be a little confusing. If you see the * in a DECLARATION statement, a pointer is being declared for the first time. AFTER that, when you see the * on the pointer name, you are DEREFERENCING the pointer to get to the target.


DYNAMIC VARIABLES

Just like I said above, dynamic variables are created during program execution. This is where the real power of pointers are. There are two operators associated with dynamic variable: new and destroy.

To allocate space dynamically, use the unary operator new, followed by the type being allocated.

new int;        // dynamically allocates an int 
new double; // dynamically allocates a double

The new operator returns the starting address of the allocated space, which can then be stored in a pointer.

int *p;        // declare a pointer p 
p = new int; // dynamically allocate an int & load address into p

To reach the dynamically created target, dereference the pointer.

int *p = new int;	// dynamic integer, pointed to by p
*p = 10; // assigns 10 to the dynamic integer
cout << *p; // prints 10

int *my_ptr = new int;
*my_ptr = 123;
my_ptr = new int;
*my_ptr = 101;
Image for post
Image for post

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store