DEV Community

mikkel250
mikkel250

Posted on

Advanced pointer operations in C

Overview and review

There are several basic operations that can be performed on pointers in C. You can:

  • Assign an address to a pointer. Assigned value can be:

    • An array name
    • A variable preceded by an address of operator &
    • Another, second, pointer
  • You can dereference a pointer:

    • the * operator gives the value stored in the pointed-to location
  • You can take a pointer address:

    • the & operator tells you where the pointer itself is stored
  • You can perform pointer arithmetic:

    • Use the + operator to add an integer to a pointer or a pointer to an integer (an integer is multiplied by the number of bytes in the pointed-to type and added to the original address)
    • Increment a pointer by one (useful in arrays when moving to the next element)
    • Use the minus - operator to subtract an integer from a pointer (integer is multiplied by the number of bytes in the pointed-to type and subtracted from the original address)
    • There are two forms of subtraction for pointers:
      • you can subtract one pointer from another to get an integer
      • you can subtract an integer from a pointer to get a pointer
    • decrementing a pointer by one (useful in arrays for going back to previous element)
  • You can find the difference between two pointers -- this will show how far apart two elements are in the same array.

  • You can use the relational operators to compare the values of two pointers (but they must be of the same type).

  • Note that when incrementing and decrementing pointer, the computer does not keep track of whether the pointer still points to an array element, which can cause an array "out of bounds" error.

  • The value referenced by a pointer can be used in arithmetic expressions as long as it is a type of number (covered in the post on pointer basics). One thing to note is that this does not change the value of the address it points to, just the value stored at that address.

When receiving input
  • When using scanf to input numeric types, the & operator is used to store the address of the variable. When using a pointer, the & is not necessary because the pointer contains an address, and can be used as an argument for scanf():
int value = 0;
int * pvalue = &value;

printf("Input an integer: ");
scanf("%d", pvalue);

printf("you entered %d \n", value);
// the value can be output directly since the address is stored.
Enter fullscreen mode Exit fullscreen mode
Testing for NULL

First, a quick warning.
Do not dereference an uninitialized pointer!

// an uninitialized pointer
int * pt;

// storing a value and dereferencing the pointer will cause a terrible error
*pt = 5;
Enter fullscreen mode Exit fullscreen mode

The second line above means to store the value 5 in the location to which pt points. Since it is uninitialized, pt has a random value, and there is no way of knowing where the 5 will be placed.

  • Creating a pointer only allocates memory to store the pointer itself, it does not allocate memory to store data.
  • Before using a pointer, it should be assigned a memory location that has already been allocated.
    • assign the address of an existing variable to the pointer, or
    • use malloc() to allocate the memory first (covered later).

Assigning an uninitialized pointer might go somewhere harmless, but it might overwrite data or code, or cause the program to crash. For this reason, it is recommended to always initialize pointers when they are declared, and use NULL if no value will be assigned immediately. It is also good practice to test for NULL when dealing with pointers. The value zero 0 and NULL are equivalent in C.

int * pval = NULL;
// is equivalent to
int * pval = 0;

// and can be tested using
pval == NULL;
// or
if(!pval)
{
  // code
}
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)