In C, the name of an array acts as a pointer to the first element of the array.
- So, an array name can be used as a pointer.
- You can use pointers to traverse and manipulate arrays efficiently.
- If arr is an array, then arr is equivalent to &arr[0].
- *(arr + i) gives the value of arr[i].
Example:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // ptr points to first element of arr
for(int i = 0; i < 5; i++) {
printf("arr[%d] = %d, *(ptr + %d) = %d\n", i, arr[i], i, *(ptr + i));
}
return 0;
}
Discussion 0