When I started programming (first language in use:C#) I new that pointers existed but I haven’t really had the time to tackle that field although I really wanted to, but somehow ignored that for a while. In C# I didn’t need pointers for anything, you could to everything without them. Later when I was helping my former high school colleague with C I realized that I will have to learn them one way or the other to help him better understand programming and eventually get a better grade.
I think that everyone has heard of pointers as being a really complicated field to grasp so I took extra caution when started to learn them. I must specify the link where I picked the stuff pretty quickly is really awesome and you will see immediately that the author of the article really took some time to explain them and even made a little quiz which is, again, awesome (he uses c++ but this isn’t relevant in order to understand them): Click here
Here goes my explanation on pointers:
You can think of pointers as arrows which hold an address and therefore the arrow knows where to go. Pointers point to a memory location which stores data. Bear in mind that he computer memory consist of small cells everyone having the length of 1 byte (8bits) and each cell has a unique address. REAL DEFINITION: a pointer is a variable whose value is the address of another variable (definition url: click here)
If you have ever written a decent ms office word article which involves footnotes or if you think of the Wikipedia numbers that are placed at the end of a sentence usually after the dot(.). When you click on the number it will lead you to some further explanation about the marked sentence which is at the end of the Wikipedia document (the further explanation is often accompanied by a link). the little number that you clicked on leads you to some other value in this case associated with the little clicked number. We can say that the little number contains an address which new where to point if clicked. This is exactly how pointers work. They are storing a value which is an address which leads you to another value. To make it 100% simple: they are addresses and everyone knows what addresses are for in real life.
Okay, why do we use pointers, why don’t we just directly store values inside the memory?
Before answering this question I must tell you what I always heard about pointers. Experienced programmers were always saying that pointers are fast. I’ll never forget that because that is the only thing that I’ve heard about pointers from someone else. Well, the reason why there are fast would explain their usage wouldn’t it?
So why use them?
answer: because they save memory and time.
Okay, so how do they safe memory?
Answer: remember what the real definition of pointers is? Pointers store the address of the variable which points to the storage location. Just imagine having a large chunk of data. let this big data be an array. If you want to copy this array to another array and not use pointers you would most definitely store all of the elements of the previous array to a new array rather than doing that you can use the advantage of pointers and create a pointer which will hold the address of the array thus not creating another array and consuming a lot of memory again. Also the memory is heavily connected with the time variable. When you occupy memory it definitely takes up some time. Now, think of populating memory with a lot’s of data. This would take a pretty much amount of time and you don’t want this process to happen multiple times for the same data.
Okay, so now we can see that pointers are efficient, now how do they work and how do you use them?
First to clear things up for real beginners. the symbol (*) means: fetch (get) the value of the pointer p. the symbol (&) means: fetch the address of the variable which is located right after the & symbol.
So, once again value=*, address=&
The above code has a pointer p declared after that we are storing an integer 8 in variable a. The line “p=&a” means that we are giving the address to the pointer to point! This is what pointers are for, they are always storing the address of a specified variable. We discussed that previously.
Here is a little illustration for that:
The most important thing that you have to remember here is that a pointer always points to a memory location which was defined previously by giving the address to the pointer. This means, no matter how much the value of a location changes which a pointer points to, the pointer will always get the current value of that location. Let’s say that after reserving a space (certain amount of memory) and then declaring a pointer to point to this reserved space, you can alter that location numerous amount of time and the last value specified for this location will be the value of the pointer since it point’s at it always.
See how pointers are easy actually?
Let’s put some more examples:
int a = 5, b = 10;
int *p1, *p2;
p1 = &a;
p2 = &b;
*p1 = 10;
In this example, you can see that you can change the value of a variable by not only changing it to just put another value in it, like you would normally do, for example: a=3 (a variable changed to value 3). You can also modify it by putting it in a pointer *p1 = 10;. This line of code changed variable a from 5 to 10 because earlier, to be more precise at line 3 we said that p1=&a;, and by that we stored the address of a inside p1. Now, every-time we give another value to p1 we will automatically change the value of variable a.
We can see that the same applies for an array as well. We’ve declared an array and a pointer p. Next we gave the variable p the address of the 4th element in the array and after that we changed it to 10 using a pointer. Now the array and index are both equal to 10 instead of 7.
This was an introduction to pointers. We mentioned their usage benefits and how to use them in code. Later articles will cover double pointers (pointers which point to pointers which then point to the value, I think you already understand double pointers), functions and pointers as arguments in a function and some more “confusing” pointers code.