Detailed Use of Arrays in C Language

Today, let’s talk about the C language. When I first started learning, I was quite confused; it seemed simple, but I always encountered various problems when using it. After many years of trial and error, I would like to share some of my insights with you.

Here is a detailed explanation of the use of arrays in C language:

Definition of Arrays

  • One-Dimensional Array: The general form is type arrayName[arraySize];, where type is the data type of the elements in the array, such as int, char, float, etc.; arrayName is the user-defined name of the array; arraySize must be a positive integer constant that indicates the number of elements in the array.

  • Two-Dimensional Array: The form is type arrayName[arraySize1][arraySize2];, which can be understood as a table with arraySize1 rows and arraySize2 columns, where each element has a unique row and column index.

  • Multi-Dimensional Array: The definition method is similar to that of a two-dimensional array, such as a three-dimensional array type arrayName[arraySize1][arraySize2][arraySize3];, and so on. It is usually used to represent more complex data structures, but as the dimensions increase, memory usage and operational complexity will also significantly increase.

Initialization of Arrays

  • Complete Initialization: When defining an array, initial values are specified for each element of the array. For example, int arr[5] = {1, 2, 3, 4, 5}; initializes the 5 elements of the array arr to 1, 2, 3, 4, and 5, respectively.

  • Partial Initialization: Only some elements of the array are given initial values, while the remaining elements will be automatically initialized to 0 or null. For example, int arr[5] = {1}; initializes arr[0]=1, while arr[1] to arr[4] are initialized to 0.

  • Size-Omitted Initialization: The size of the array can be omitted during initialization, and the compiler will automatically infer the size of the array based on the number of elements in the initialization list. For example, int arr[] = {1, 2, 3, 4, 5}; means the size of the array arr is 5.

Accessing Array Elements

  • Accessing by Index: The index of an array starts from 0, so for an array of size n, its valid index range is from 0 to n-1. You can access elements in the array using the form arrayName[index], where index is the index. For example, for the array int arr[5] = {1, 2, 3, 4, 5};, you can access the first element 1 with arr[0] and the third element 3 with arr[2].

  • Accessing by Pointer: The array name itself is a pointer to the first element of the array. You can access other elements in the array by performing pointer arithmetic. For example, for the array int arr[5];, arr and &arr[0] are equivalent, both pointing to the first element of the array. You can access the i-th element using *(arr + i), where i is the offset.

Traversing Arrays

  • Using Loops to Traverse: Typically, a for loop is used to traverse an array. For example, to traverse an integer array int arr[5] = {1, 2, 3, 4, 5};, you can write:

for(int i = 0; i < 5; i++){<br/>    printf("%d ", arr[i]);<br/>}
  • Using Pointers to Traverse: Utilize the array name as a pointer, and traverse the array by continuously moving the pointer. For example:

int arr[5] = {1, 2, 3, 4, 5};<br/>int *p = arr;<br/>for(int i = 0; i < 5; i++){<br/>    printf("%d ", *p++);<br/>}

Arrays as Function Parameters

  • Passing Array Name: In C language, when the array name is passed as a function parameter, it actually passes the address of the first element of the array. Operations on the array inside the function will directly affect the original array. For example:

void printArray(int arr[], int size){<br/>    for(int i = 0; i < size; i++){<br/>        printf("%d ", arr[i]);<br/>    }<br/>}<br/><br/>int main(){<br/>    int numbers[5] = {1, 2, 3, 4, 5};<br/>    printArray(numbers, 5);<br/>    return 0;<br/>}
  • Passing Pointer: You can also directly pass a pointer to the array as a function parameter. For example:

void printArray(int *arr, int size){<br/>    for(int i = 0; i < size; i++){<br/>        printf("%d ", *(arr + i));<br/>    }<br/>}<br/><br/>int main(){<br/>    int numbers[5] = {1, 2, 3, 4, 5};<br/>    printArray(numbers, 5);<br/>    return 0;<br/>}

Common Array Operations

  • Modifying Array Elements: You can modify elements in the array directly by index or pointer. For example, for the array int arr[5] = {1, 2, 3, 4, 5};, you can change the third element to 10 with arr[2] = 10;.

  • Copying Arrays: You can use a loop to copy the elements of one array to another array one by one. For example:

int arr1[5] = {1, 2, 3, 4, 5};<br/>int arr2[5];<br/>for(int i = 0; i < 5; i++){<br/>    arr2[i] = arr1[i];<br/>}
  • Sorting Arrays: Common sorting algorithms include Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, etc. For example, using Bubble Sort:

void bubbleSort(int arr[], int size){<br/>    for(int i = 0; i < size - 1; i++){<br/>        for(int j = 0; j < size - i - 1; j++){<br/>            if(arr[j] > arr[j + 1]){<br/>                int temp = arr[j];<br/>                arr[j] = arr[j + 1];<br/>                arr[j + 1] = temp;<br/>            }<br/>        }<br/>    }<br/>}<br/><br/>int main(){<br/>    int numbers[5] = {5, 4, 3, 2, 1};<br/>    bubbleSort(numbers, 5);<br/>    for(int i = 0; i < 5; i++){<br/>        printf("%d ", numbers[i]);<br/>    }<br/>    return 0;<br/>}

Leave a Comment