An Intro To Pointers

Before we talk about what are pointers,lets talk a bit about memory one of the most important part of programming. Whenever you code something instruction gets loaded into memory and from there the CPU fetches it and executes it. Think of memory as a single contiguous row of houses each where each house has a unique address assigned to it. That’s how we address memory. Instructions gets loaded into specific memory locations and from there the CPU executes it.

What are Pointers?

Think of pointers as integers that holds memory addresses. That’s it. There maybe types assigned to pointers like float,int,char but at the end of the day all that is meaningless and its only there to make reading code easier so that we know that what type of variable is being pointed to. But since pointers hold memory addresses and memory addresses are integers, so all pointers are variables which hold memory addresses.

Do we need Pointers?

Well no. We can perfectly code without using pointers ever but it doesn’t hurt to know how they work.

Let’s Code

For this tutorial I’m using Visual Studio 2017. I’m mentioning this as I would like to show how memory works. Here’s a simple code snippet…

#include "pch.h"
#include <iostream>

int main()
{
	int* p;
}

The variable p above is a pointer variable and it is supposed to hold a memory address. Notice how we declared it using an asterix after int, that’s how we define pointers.

Now let’s look at the code below..

int main()
{
	int k = 10;
	int* p=&k;
}

Notice how we declared a variable k and assigned a value of 10 to it? Well this variable k has a memory address assigned to it. We can access its memory address using the ampersand sign. So &k stands for variable k’s memory address and we are assigning it to the pointer variable p. So now p holds the memory address of k.

Let’s run some code…

#include "pch.h"
#include <iostream>

int main()
{
	int k = 10;
	int* p=&k;
	std::cout << p;

}

And this outputs 001FFC28 in the console, which is the memory address of the variable k.

Now let’s look at how pointers actually work in memory. For this set a breakpoint in visual studio as shown below.

Capture 3

Now run the code and when breakpoint is reached access the debug-window-memory menu and see bring out the memory window. If you hover over the p variable at this point of time you would see that its value is shown which is the memory location of k. Paste this into the memory window and you’ll see something like this…

Capture 4

Notice the address bar, we’ve pasted the value of p into it which is the memory address of k. As you can see in the first row the first four numbers are 0a 00 00 00 which actually translates to 10 in decimal. There are 4 numbers as we know int is four bytes long. So we can see that the variable p actually holds the memory address of k.

Manipulating memory

Now lets change the value of k without directly changing it. Look at the code below…

#include "pch.h"
#include <iostream>

int main()
{
	int k = 10;
	int* p=&k;
	*p = 5;
	std::cout << k;

}

If you run the code above, you’d see that 5 gets printed in the output console. Now why’s that? Isn’t k supposed to be 10? Well no. Note the line *p=5, here we are accessing the variable that p is pointing to which is k and assigning 5 to it. So k becomes 5,without directly accessing it.

Well that’s all for this intro to pointers. There will be more posts in the future where we will look at pointers at more detail when we discuss about heap and stack. Cheers!

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE

Related :

Follow :