How can pointers in the C language point to arrays?
In C language, pointers can point to arrays in two ways:
- Using array name as a pointer: In C language, the array name can be seen as a pointer to the first element of the array. For example, consider the following code:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // 使用数组名作为指针
In the above code, arr can be seen as a pointer pointing to the first element 1, so assigning arr to pointer ptr will make ptr point to the first element of the array arr.
- I only need one option to paraphrase the following natively in English.
- Can you speak louder?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // 使用取地址操作符
In the above code, &arr[0] represents the address of the first element 1 in the array arr. By assigning this address to the pointer ptr, it can now point to the first element of the array arr.
Pointers can access other elements in an array by increasing or decreasing their values, whether by using the array name or the address operator. For example, the increment operator “++” can be used to access the next element, such as ptr++ representing the pointer moving one element forward.