How are pointers to arrays represented in the C language?
In the C language, the array name itself is a pointer to the first element of the array. The array name can be used as a pointer to represent the array.
For example, there is an array of integers:
int arr[5] = {1, 2, 3, 4, 5};
Arrays can be represented using pointers.
int *ptr = arr;
In the code above, ptr is a pointer to an integer, pointing to the first element of the array arr. Array elements can be accessed by dereferencing the pointer.
printf("%d\n", *ptr); // 输出:1
To access the next element of an array, you can increment the pointer.
ptr++;
printf("%d\n", *ptr); // 输出:2
Similarly, you can also access array elements using the pointer’s index operator [].
printf("%d\n", ptr[2]); // 输出:3
Here, ptr[2] is equivalent to *(ptr + 2), indicating the third element of the element pointed to by the pointer ptr.
In summary, pointers to an array can be declared either by using the array name or by using * to declare a pointer to the array, and then using the pointer to access the elements of the array.