C language array

This is an explanation of arrays in C language. Arrays in C are static arrays of fixed length.

# Array declaration
Type variable name [number of elements];

This is a sample that declares an int32_t type array of length 3.

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

int main (void) {
  
  // Declaration of array
  int32_t nums [3];
}

The elements of the array have not been initialized. Note that it is not initialized with 0.

Array initialization

You can initialize the array at the same time as the declaration. The number of elements can be omitted.

# Array declaration and initialization
Type variable name [] = {initial value of element 0, initial value of element 1, initial value of element 2};

This is a sample that initializes an array of type int32_t at the same time as the declaration.

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

int main (void) {
  
  // Declaration of array
  int32_t nums [] = {3, 5, 9};
}

Initialize array with 0

I will introduce a simple syntax for initializing an array with 0. This is an initialization syntax that takes advantage of the C language specification that 0 (all bits are 0) is used as the initial value if no initial value is specified.

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

int main (void) {
  
  // Array declaration and 0 initialization --int32_t
  int32_t nums [3] = {0};
  
  printf("%d\n", nums [2]);

  // Array declaration and 0 initialization --double
  double dnums [3] = {0};
  
  printf("%f\n", nums [2]);
}

Output result.

0
0.000000

Manage array length

In C, you can't get the length of an array. You have to manage it yourself. This is because arrays in C are pointers to memory and have no length information.

Therefore, I will write and manage it myself.

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

int main (void) {
  
  // Declaration of array
  int32_t nums [] = {3, 5, 9};
  
  // Array length
  int32_t nums_length = 3;
}

Get and set the elements of the array

To get and set the elements of the array, use the following syntax. Element numbers start at 0.

# Get array elements
Array [element number]

# Array element settings
Array [element number] = value

This is a sample setting for getting the elements of an array. If you mistakenly exceed the element number at the end of the array, you are accessing a memory area that should not be accessed, so be careful.

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

int main (void) {
  
  // Declaration of array
  int32_t nums [] = {3, 5, 9};
  
  // Get array elements
  int32_t num1 = nums [1];
  printf("%d\n", num1);
  
  // Setting the elements of the array
  nums [1] = 12;
  printf("%d\n", nums [1]);
}

Process all elements of the array

Let's output all the elements of the array using for statement.

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

int main (void) {
  
  // arrangement
  int32_t nums [] = {3, 5, 9};
  
  // Array length
  int32_t nums_length = 3;
  
  // Output all elements of the array
  for (int32_t i = 0; i <nums_length; i ++) {
    printf("%d\n", nums [i]);
  }
}

Output result.

3
Five
9

Array data is stored on the function call stack

The array data is stored on the function's call stack. When you make a function call, the call stack is reserved for local variables, but this area is used. This is in contrast to using malloc to allocate the heap.

Memory access on the call stack is more local and therefore has better performance than the heap.

Differences from Perl arrays

An array in C is a static array that reserves memory on the function's call stack. The size of the elements in the array is the size of the element type (in the case of a structure, the size including padding).

On the other hand, the Perl array is a dynamic array that reserves memory on the heap. The size of the elements in the array is the size of the SV type.

Associated Information