How to call an array in C language?

In C language, elements in an array can be accessed and modified using the array name and index. Here are some common methods:

  1. Access array elements using an index.
int arr[5] = {1, 2, 3, 4, 5};
int element = arr[2];  // 访问第3个元素,值为3
arr[0] = 10;  // 修改第1个元素的值为10
  1. Traverse the array using a loop structure.
int arr[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
    printf("%d ", arr[i]);  // 输出每个元素的值
}
  1. Passing an array as a parameter in a function.
void printArray(int arr[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("%d ", arr[i]);  // 输出每个元素的值
    }
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printArray(arr, 5);  // 调用函数打印数组
    return 0;
}

These are some basic methods, in practical applications, more complex operations can be carried out according to specific needs.

bannerAds