“Pointer” it is one of those words that many programmers just skim over when they come across it in a book or during one of their courses. This single word is one of the most important concepts to gather an understanding for in order to be a decent developer. I, as many, never had a required course that was taught in C/C++. This was due to the seemingly nationwide transition away from C++ toward the Java realm. At the University of Iowa, a student can go through all courses without really learning what a pointer is and why its important. So here I am to save the day, I am going to explain (the best way my limited grammatical skills will allow) about pointers for all those programmers out there who are sitting clueless trying to understand or write their first C/C++ program.

What is a pointer?

Formal Definition: a data type whose value refers directly to another value stored elsewhere in the computer memory using its address.
 
Ok simple enough right? Moving on to something less obvious. Hell no! I will explain in the most humanly understandable form (known as examples) on what a pointer is.

What is a pointer? (Idiot’s Guide Edition)

Abstract: My name is Justin Smestad. Some of my friends call me JSTAD. So what is JSTAD? Yes its a nickname, but more formally it is an alias. If you were to address something to ‘JSTAD’ you would want this name to address me, Justin Smestad. So you would say JSTAD = Justin Smestad (or JSTAD -> “Justin Smestad”). This essentially is what a pointer is doing inside a program.
 
Formal: Given this sample code
 
void int main()
{
    int x = 4;
    printf(”%d”, x);
}
We can see that x holds the value 4. Now if we wanted to add another variable that will always have the same value as x (lets call this y). We would use a pointer.
void int main()
{
    int x = 4;
    int *y;
    y = x;
    printf(”%d”, x);
}
 

So what did we do?

Well in the above code we have defined a pointer y (a pointer is any data type defined with an asterisk). We then did as we said above, we are setting y to always equal x. This is like saying in our example, we want to set JSTAD to always be the person Justin Smestad. This is the basic idea behind a pointer, it simply is an alias to a data type. The only thing the pointer y holds is the memory location where x is being stored. Pretty simple eh? Don’t worry if your still lost in the point of pointers (pun intended), I will show you some quick reasons they are incredibly useful.
 

Why do we use/need pointers?

If you were going to implement something like a linked list, you are going to use pointers. If you are going to implement anything where your data can dynamically grow or shrink then you are going to use pointers. If you are going to declare ANY data structure that is either not fully declared at instantiation or pass anything by reference then your most likely using pointers.
 
Get it? POINTERS ARE IMPORTANT. To show you why pointers are useful lets say we are going to declare a list of objects. Nothing very advanced we just want a list of numbers, but we do not know how many numbers there will be. [Many Java or C# programmers would jump on the horn and yell at us to use Collections or something similar, but what they fail to realize something very important. Those data structures are using POINTERS!] What we need to declare the root object of our list using List l = new List(); In order to add things to our list, we need to be able to point to the next object. Since memory is never stored sequentially, our added numbers can be stored in any number of locations. We use pointers to keep track of these location addresses and give the data structure the effect of things being stored sequentially. So when we do List.get(3), it gets the pointer from List[0] then uses its pointer to go to List[1], then uses List[1] to go to the location of List[2], …all the way to the element we want to access. Pointers are the reason Java, C# and other languages have features we take for granted like Collections, DynamicArray, and many others.
 
Now don’t give up yet. It is still important to understand pointers even if you have taken an oath never to code a line of C/C++ ever in your entire career. Pointers are the reason your favorite OO language can do those great method parameter tricks, “pass by reference” and “pass by value”. If you are unfamiliar with these two terms, I will quickly explain what they do. Passing by reference is saying that when you call a method (lets say add(a, b);) the values a and b are the original version of those variables, so if they are changed inside add to a different value, the change is scene throughout the program. Pass by value is the opposite where any changes that happen inside the method are “thrown into the trash” once the add method is finished running (think of it as a duplicate copy, changing the duplicate doesn’t change the original like pass by reference). Alright, anyway pass by reference is done through pointers (even though languages like Java hide this from you). What java does internally is takes your method call add(a,b); and changes them into add(a, *b); This means Java says “pass the memory address of a and b to add” Because the system is using a and b’s memory addresses it is editing the original copy of the variable. Pass by Value on the other-hand takes the values in a and b, copies them to a new memory location and then uses those values as the data.

Why should I care?

If you can grasp the understand of pointers, you can begin to program having a deeper knowledge of what the system is doing. Many popular OO languages hide this information from you so you are none the wiser of what is happening in the background (called “programming magic” or “voodoo”). Well its great they allow you to forget about handling pointers (and the pains that sometimes come from them), but when a program has compile or run time errors (such as Segmentation Fault) you are going to be dealing with a pointer issue. If you do not have a good grasp of what a pointer is or what it is doing your productivity level is going to suffer greatly. In addition, with a good understanding of pointers it will allow you to do some amazing things with pointer manipulation that you never thought was possible!
 
If you want a more in-depth look at pointers, I highly suggest heading over toStandford’s Binky Pointer Fun Video which will reiterate everything I have said here and covers even more in less time. Also the wikipedia article on pointers is a good place to go as well.