Arrays and pointers are closely related in C. In fact, the name of an array is essentially a pointer to the first element of that array. When you use the name of an array (like arr), it represents a pointer to the first element of that array.
Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Array with 5 elements
int *ptr = arr; // Pointer points to the first element of the array
printf("First element: %d\n", *ptr); // Output: 10
return 0;
}
Output:
First element: 10
Explanation:
- arr points to the first element of the array, arr[0].
- ptr is a pointer that holds the address of arr[0], so dereferencing ptr gives you the value 10.
C pointers and arrays – Interview Questions
Q 1: How are pointers related to arrays?
Ans: An array name acts as a pointer to its first element.
Q 2: Can we change the array base address?
Ans: No, array name is constant.
Q 3: How do you access array elements using pointers?
Ans: Using pointer arithmetic like *(ptr + i).
Q 4: Are arrays passed by value or reference?
Ans: Arrays are passed by reference.
Q 5: What is the difference between arr and &arr?
Ans: arr points to the first element; &arr points to the entire array.
C pointer and array – Objective Questions (MCQs)
Q1. What is the relationship between array name and pointers in C?
Q2. What does *(arr + i) represent in C?
Q3. What will be the output of the following code?
int arr[3] = {5, 10, 15};
int *p = arr;
printf("%d", *(p + 2));
Q4. What is the type of array name in C?
Q5. Which expression gives the address of the first element of an array arr?