How to point a pointer to an array in the C language?

Pointers can point to arrays using the array name.

For example, suppose we have an array of integers:

int arr[5] = {1, 2, 3, 4, 5};

You can use a pointer to point to this array.

int* ptr = arr;

Here, ptr is a pointer to an integer, pointing to the first element of the array. Other elements can be accessed by incrementing the pointer.

printf("%d\n", *ptr);  // 输出第一个元素:1

ptr++;  // 指针递增
printf("%d\n", *ptr);  // 输出第二个元素:2

ptr += 2;  // 指针递增2
printf("%d\n", *ptr);  // 输出第四个元素:4

To access array elements through pointers, you can use the dereference operator *, which means *ptr represents the value pointed to by the pointer ptr.

bannerAds