Pointers and Arrays in C Programming
Pointers and Arrays in C Programming
Introduction to Pointers and Arrays
Pointers and arrays in C programming are closely related concepts. An array name itself acts like a pointer to its first element.
Understanding pointers and arrays in C programming helps in efficient data handling and memory access.
Relationship Between Pointers and Arrays
In C programming, the name of an array represents the address of its first element.
Example
int *ptr = arr;
Here:
arrpoints to the first elementptrstores the same address
Accessing Array Elements Using Pointer
You can access array elements using pointer arithmetic.
Example
int main() {
int arr[3] = {5, 10, 15};
int *ptr = arr;
printf(“%d\n”, *ptr); // 5
printf(“%d\n”, *(ptr + 1)); // 10
printf(“%d\n”, *(ptr + 2)); // 15
return 0;
}
Pointer Notation vs Array Notation
Both notations can be used to access elements.
Example
int main() {
int arr[3] = {1, 2, 3};
printf(“%d\n”, arr[1]); // Array notation
printf(“%d\n”, *(arr + 1)); // Pointer notation
return 0;
}
Traversing Array Using Pointer
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
int i;
for(i = 0; i < 5; i++) {
printf(“%d\n”, *(ptr + i));
}
return 0;
}
Passing Array to Function Using Pointer
Arrays are passed to functions as pointers.
Example
void printArray(int *arr, int size) {
int i;
for(i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
}
int main() {
int arr[4] = {1, 2, 3, 4};
printArray(arr, 4);
return 0;
}
Modifying Array Using Pointer
void update(int *arr) {
arr[0] = 100;
}
int main() {
int arr[3] = {10, 20, 30};
update(arr);
printf(“%d”, arr[0]); // Output: 100
return 0;
}
Key Points About Pointers and Arrays
Important Notes
- Array name is a pointer to first element
- Pointer arithmetic works on arrays
- Arrays are passed by reference
- Memory is contiguous
Advantages of Using Pointers with Arrays
Key Benefits
- Efficient data access
- Faster operations
- Easy traversal
- Useful in function calls
Common Mistakes
Avoid These Errors
- Accessing out of bounds
- Misusing pointer arithmetic
- Confusing pointer and array syntax
Start Learning C Programming
Practice pointers and arrays together to master memory handling in C programming.
Summary
Pointers and arrays in C programming are closely related. Arrays behave like pointers, and pointer arithmetic helps in efficient data manipulation.
FAQs
What is relationship between pointers and arrays?
Array name acts as a pointer to first element.
Can we use pointer instead of array?
Yes, for accessing elements.
How are arrays passed to functions?
As pointers.
What is *(arr + i)?
It accesses array element using pointer notation.



