Pointers — The Basics
Pointers are a wonderful thing! In most languages these are hidden from the user, but they can be very powerful tools if you know how to use them. First what does a pointer look like:
In this example we create a variable called foo this is just a regular integer.
Then we define the pointer. A pointer just points to memory. This is why when we create the pointer we have to use the &. The & returns the address of the variable named after it so in this case it returns the memory address of foo, and now that memory address is what bar is storing. How would a memory address of something help? Well let’s look at another example
What do you think this program prints? It prints 8. Some things to note, when modifying a pointer to access its value you need to put a * in front of it. This tells the program to take the value at that memory address and don’t
modify the address itself (which you can do). If you were to take out the * in this example you would be adding 5 to the memory address which would move what the pointer is pointing at. At that point you would have no clue what the value of the pointer is anymore, and you would not get the result you want. In this example the value of foo would never be changed so the add_five function would be completely useless it would just create a pointer then move that pointer and stop. Also to get the address of a value you already have your put a & in front of it
Without the use of pointers in this you would have to return the new value and set foo equal to the function like this:
There are some key differences here first let’s look at the function. It now has a return type of int and it returns num after we add 5 to it. In the main function we create foo just like before but when we call the add_five function we have to set foo equal to the return value of add_five or else foo will be unaffected by the change. This is a simple example, but when you have large arrays of data you don’t want to be recreate multiple sets of it. It’s slow and memory intensive.
Let’s use pointers to create some strings:
So we start by defining a pointer, but in this case we want to make it point at memory that doesn’t have any value yet. We do this by using malloc(). This asks the operating system for free memory and then the operating system gives it to the program (assuming it has memory to give). So in the first line we have 16 bytes of free memory to do with as we choose. Now we want to fill that data, so we call strcpy(). This takes a 2 char *, so we give it “this”, and then we put buf in. This takes the value of the first string and copies it over to the second one. Now our string says this, but we need to truncate it (end the string) so c knows where the string ends. To do this we use an escape character ‘\0’, so we do buf[4] = ‘\0’. This sets the 5th character to the escape character and tells c that the string ends there.
This is all there is to know for simple pointer use, but it can get much more complex.