loading...

Advanced pointer operations in C

mikkel250 profile image mikkel250 ・3 min read

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files 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.
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;

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
}

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files in C

Posted on Dec 24 '19 by:

mikkel250 profile

mikkel250

@mikkel250

I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.

Discussion

markdown guide