Pointers in C

Pointers

Variables

To understand pointers let´s review first the concept of variable.

  • A variable has a name, and address, a type and a value:
    • The type specifies how to interpret the data stored in main memory during execution and the size of the variable.
      • If you are going to store lions then you need big cages designed for lions
      • Analogically, if you are going to store doubles then you need 8 bytes to store a double value.
      • If you are going to store squirrels then you need small cages designed for squirrels.
      • Analogically, if you are going to store unsigned chars then you need 1 byte in memory to store an unsinged char.
    • The value is the actual data stored in the variable after it has been interpreted according to a given type.
      • Leo the lion
      • 1234.32342
      • Dale the squirrel
      • 2
    • The address specifies where in main memory the variable is located.
      • In the zoo all the cages have a number.
        • The cage of Leo the lion is the number 3234.
      • In the memory all the variables have an address.
        • The address where 1234.32342 is stored is 0xA0239201
    • The name identifies the variable to the programmer.
      • For easy reference the zoo employees refers to Leo´s cage as "the cage of the Lion"
      • For easy reference the programmer refers to 0xA0239201 as local_float_variable

Pointers

Now that we understood that all variables have a memory address, you should know that we can manipulate the values of that variable directly by using the address.

As variables are friendly names for the data stored in them, Pointers are friendly names for the addresses in memory where the data is stored.

Thus, a pointer is a variable where we can store data addresses

  • We can store the address of local_float_variable in a pointer named pointer_to_float_variable
  • Example
    float local_float_variable = 1234.32342;
    float *pointer_to_float_variable; /*This is the pointer declaration, note that this pointer can only point to addresses storing float data*/
    pointer_to_float_variable = &local_float_variable; /*Here we get the address of local_float_variable and store it in pointer_to_float_variable*/
    *pointer_to_float_variable += 1; /*Here we modify the float value, now it becomes 1235.32342*/
    

Pointer Operators

We have two operators for working with pointers:

  • &
    • this operator is useful when we have a variable and we want to know it's memory address.
    • This operator is known as:

      the address of...

    • Example
      printf ("the address of local_float_variable is: \n",&local_float_variable);
      
  • *
    • we use this operator when we have a pointer and we want to know the data pointed by this pointer.
    • This operator is known as indirection operator. I name this operator

      the data pointed by...

    • Example
      printf ("the data pointed by pointer_to_float_variable is: \n",*pointer_to_float_variable);