Pointer operation

The C language has a function called pointer operation that can advance the position of an address based on the width of the data type.

This is often used in practice to advance the position of each element of an array one by one, so first pointer and arrays.

Relationship between arrays and pointers in C

In C, accessing the elements of an array is syntactic sugar for pointer arithmetic. First of all, please feel that you can access the memory area allocated by calloc function as an array.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  
  // Represent an array using pointers
  int32_t nums_length = 10;
  int32_t * nums = calloc (sizeof (int32_t), nums_length);
  
  // Assign to an array element
  nums [2] = 5;
  
  // Output array elements
  printf("nums [2]:%d\n", nums [2]);
  
  // Set element values ​​using pointer operations
  * (nums + 2) = 7;

  // Output array elements
  printf("nums [2]:%d\n", nums [2]);
  
  // Output element values ​​using pointer operations
  printf("* (num2 + 3):%d\n", * (nums + 2));
  
  free (nums);
}

Output result.

nums [2]: 5
nums [2]: 7
* (nums + 2): 7

See that "nums [2]" is syntactic sugar for "* (nums + 2)".

nums represents the start address of memory allocated by calloc.

The entity is acquired by "* (nums + 2)". The acquisition of the entity is explained in pointer.

Take a look at pointer operations

Pointer operation advances the address by multiplying the specified value by the value in units of the size that can be obtained by sizeof operator.

The operation "nums + 2" seems to add 3, but since num2 is a int32_t type, it is actually "sizeof (" int32_t) * 3 ”will increase.

Let's actually output it. The address can be output with the "%p" format specifier. It is output in hexadecimal.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  
  // Represent an array using pointers
  int32_t nums_length = 10;
  int32_t * nums = calloc (sizeof (int32_t), nums_length);
  
  printf("nums:%p\n", nums);
  printf("nums + 1:%p\n", nums + 1);
  printf("nums + 2:%p\n", nums + 2);
  printf("nums + 3:%p\n", nums + 3);
  
  free (nums);
}

This is the output result in my environment. Since the size of int32_t obtained by the sizeof operator is 4, the address increases in units of this.

nums: 0x4538010
nums + 1: 0x4538014
nums + 2: 0x4538018
nums + 3: 0x453801c

Also try int64_t to see the difference.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  
  // Represent an array using pointers
  int64_t nums_length = 10;
  int64_t * nums = calloc (sizeof (int64_t), nums_length);
  
  printf("nums:%p", nums);
  printf("nums + 1:%p, nums + 1);
  printf("nums + 2:%p, nums + 2);
  printf("nums + 3:%p, nums + 3);
  
  free (nums);
}

This is the output result. Since the size of int64_t obtained by the sizeof operator is 8, the address will increase in units of this.

nums: 0x6c39010
nums + 1: 0x6c39018
nums + 2: 0x6c39020
nums + 3: 0x6c39028

Is there any reason to use pointer arithmetic when it can be accessed as an array?

Well, not much. I think the array is more intuitive and readable.

Pointer operation sample

As a sample for frequent use, I will write a thing to access the characters contained in the string in order.

Since the character string in C language ends with "\ 0", I sometimes write that the pointer points to the character position and processes in order.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  
  // string
  const char * message = "Hello World!";
  
  // Get the address at the beginning of the string
  const char * ch_ptr = message;
  
  // Repeat until the value pointed to by the pointer becomes \ 0
  while (* ch_ptr! ='\ 0') {
    
    // 1 character output
    printf("%c", * ch_ptr);
    
    // Advance the pointer
    ch_ptr ++;
  }
  printf("\ n");
}

This is the output result.

Hello World!

Associated Information