Pointers & Arrays in C

Pointers & Arrays in C


Introduction

In C programming, pointers and arrays are closely related.
An array name itself acts like a pointer to its first element.

👉 Understanding this relationship is very important for efficient programming.


Relationship Between Pointers & Arrays

👉 Array name = address of first element

int arr[3] = {10, 20, 30};

👉 arr is same as &arr[0]


Using Pointer with Array

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;

printf("%d\n", *ptr); // arr[0]
printf("%d\n", *(ptr + 1)); // arr[1]
printf("%d\n", *(ptr + 2)); // arr[2]

return 0;
}

Output

10
20
30

Accessing Array Elements

👉 Two ways:

arr[n] // normal way
*(ptr + n) // pointer way

👉 Both are same


Example: Loop Using Pointer

#include <stdio.h>

int main() {
int arr[] = {5, 10, 15, 20};
int *ptr = arr;
int i;

for (i = 0; i < 4; i++) {
printf("%d ", *(ptr + i));
}

return 0;
}

Output

5 10 15 20

Pointer Arithmetic with Arrays

👉 Pointer moves based on data type size

ptr + 1 // moves to next element

Array as Pointer Example

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3};

printf("%d\n", arr[0]);
printf("%d\n", *(arr + 0));

return 0;
}

Output

1
1

Comparison Table

Feature Array Pointer
Meaning Collection of elements Stores address
Access arr[n] *(ptr + n)
Flexibility Fixed size Flexible

Important Notes

  • Array name cannot be changed
  • Pointer can be reassigned
  • Both use contiguous memory

Common Mistakes

  • ❌ Confusing array and pointer
  • ❌ Wrong pointer arithmetic
  • ❌ Out-of-bounds access
  • ❌ Misusing * operator

Pro Tips

  • ✔ Practice pointer + array problems
  • ✔ Use pointers for efficient coding
  • ✔ Understand memory layout
  • ✔ Learn with examples

Conclusion

Pointers and arrays are deeply connected in C. Understanding this concept helps in writing efficient and optimized programs.

Master this to become strong in C programming.

👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.

Comments

Popular posts from this blog

Introduction to C Programming

Operators in C

Input & Output in C