Pointers in C

Pointers in C are variables that hold an address in the memory. That address is the location of another variable.

But why are pointers so difficult understand, yet so popular in C?
 - Because they give great powers in your hands. Remember from the previous lesson on functions - usually when we call a function with parameters, that function works with copies of the arguments. So the function couldn't change the variables that we send as arguments. However, if the argument is a pointer to a variable, the function can work with and change the original variable.

Advertise on this site. I promise you will like the rates :)

Create a pointer variable

The syntax to create and assign a pointer is as follows:
    <type> * <name> = &<name of the pointed variable>

    float temperature = 36.5f;
    float* pTemperature = &temperature;

Type

As you see, the pointer has a type. It must be the same as the type of the variable that it points to. Otherwise your compiler would warn you that you are trying to do initialization from incompatible pointer type. As a result the code might not compile or not behave as expected. That is because when you try to use the value hold in that address, it will try to interpret it as the type of the pointer (while it is not).

Asterisk

The asterisk says that this will be a pointer variable. The spaces around the asterisk are optional. You can write any of the following:

    float*pTemperature = &temperature;
    float* pTemperature = &temperature;
    float *pTemperature = &temperature;
    float * pTemperature = &temperature;

I prefer the second version, because that way it stays like a part of the type and is separated from the name.

Ampersand

The & symbol in front of the variable takes the address of the variable, instead of its actual value. This way, we assign the pointer to hold the address.

Dereferencing a pointer

The term "dereference" means that we access the value behind the address, that it points to. Here is an easy example to start with:

#include <stdio.h>

int main(int argc, char **argv)
{
    float temperature = 36.5f;
    float*  pointer = &temperature;
    printf("%.3f\n", *pointer);
    *pointer += 1.0f;
    printf("%.3f", temperature);
    return 0;
}

Here we create a pointer to the float variable "temperature". Next we use that pointer to print the value. Then we again dereference the pointer, in order to change the value of the original variable. If you run the code, you will see that the value of the original variable is now changed to 37.5 degrees.

Address vs dereference - a more detailed example

To better understand what really happens when we use the address operator and the dereference operator, look at the following example. I strongly recommend that you also run it and examine the result yourself:

#include <stdio.h>

int main(int argc, char **argv)
{
    float temperature = 36.5f;
    float*  pointer = &temperature;
    printf("The temperature variable holds the value:    %f\n", temperature);
    printf("The same value, accessed through a pointer:  %f\n", *pointer);
    printf("The address of the temperature variable is:  %p\n", &temperature);
    printf("The same address, accessed via a pointer is: %p\n", pointer);
    printf("The address of the pointer itself is:        %p\n", &pointer);
    return 0;
}

Application in functions

Like I said above - we use pointers in order to give a function access to the original variable in the memory. This allows the function to change the original value.

Remember how we read numbers from the console? We used scanf() and it required that we pass the variable that we read with the address operator &. This is exactly the same - instead of passing a copy of our variable, we pass its address and scanf() accepts that in a pointer and then it could read into our variable.

In the next example we create and call the function swap(). It accepts two pointers to int and swaps their values:

#include <stdio.h>

void swap(int* num1, int* num2);

int main(int argc, char **argv)
{
    int a = 5;
    int b = -10;
    swap(&a, &b);
    printf("a was 5, now it is : %d\n", a);
    printf("b was -10, now it is : %d\n", b);
    return 0;
}

void swap(int* a, int* b)
{
    int temp = *b;
    *b = *a;
    *a = temp;
}

What's next

There is much more to pointers like

  • void pointers
  • pointer to a function
  • pointer to a pointer

However, these are more difficult topics that I will explain in the next C tutorial - advanced level.

Previous: Functions

Next: Strings

   Search this site: