loading...

Pointers arithmetic

mikkel250 profile image mikkel250 ・2 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

The real power of pointers is when you want to sequence through the elements of an array.
Since each element in an array is an address in memory (that the pointer references), the ++ or -- operators can be used to iterate through an array.
Since the pointers by default reference the first value in an array using standard declaration, e.g. *pMyArray = myArray;, dereferencing the pointer can be used to quickly get the first value *pMyArray, and indirection can be used to access any other elements further in the array using the following syntax:

*(pMyArray + 3);
// accesses the fourth element

The same syntax can be used to assign values to an array. The two expressions below achieve the same result of assigning the number 27 to the 10th element of the array:

myArray[11] = 27;
*(pMyArray + 10) = 27;

To set the pointer to a specific element, it can be done using the 'address of' operator when it is assigned, along with the index desired: *pMyArray = myArray[3]; sets the pointer to the fourth element of the array.

An example of iterating through an array using pointers and summing the elements is below, with explanation in the comments.

int arraySum(int myArray[], const int num)
{
  int sum = 0, *ptr;
  int * const myArrayEnd = myArray + n;

  for (ptr = myArray; ptr < myArrayEnd; ++ptr)
  {
    // dereference the pointer to get the value and add the value to the sum
    sum += *ptr;
  }
  return sum;
}

int main(void)
{
  int arraySum(int myArray[], const int n);
  int values[5] = {1, 2, 3, 4, 5};

  // here, we are calling the arraySum function with the values array and pointing it to the last element (5)
  printf("The sum is %i \n", arraySum(values, 5));

  return 0;
}

Functions that process arrays actually use pointers as arguments, so you have a choice between using array notation and pointer notation for array-processing function -- it is a matter of programmer choice.
Like everything in programming, it is a trade-off. Using array notation makes it more obvious that the function is working with arrays, but pointers are closer to machine language and, with some compilers, leads to more efficient 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 22 '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