Nearby lessons

93 of 124

C - Pointers and Arrays

Pointers and Arrays is one of the foundational topics in C programming. This lesson explains Complete Program 3 — Pointers and Arrays with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Complete Program 3 — Pointers and Arrays

The name of an array is actually the address of its first element. So pointers and arrays are very close friends:

In simple words: the array name is the address of the first locker. So p = a makes p point to locker 0, and *(p + 1) reaches locker 1 — exactly like a[1].
Trainer's Note: Pointer arithmetic follows the type size: p + 1 on an int pointer moves forward by sizeof(int) bytes, not 1 byte. That is why *(p + 1) reaches the next array element correctly.
Example01
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a[5] = {10, 20, 30, 40, 50};
6 int *p = a; // p points to a[0]
7 int i;
8 
9 printf("a[0] via *p : %d\n", *p); // 10
10 printf("a[1] via *(p+1) : %d\n", *(p + 1)); // 20
11 
12 printf("Whole array via pointer: ");
13 for (i = 0; i < 5; i++) {
14 printf("%d ", *(p + i)); // same as a[i]
15 }
16 printf("\n");
17}
Output
a[0] via *p : 10 a[1] via *(p+1) : 20 Whole array via pointer: 10 20 30 40 50
📝 Key Takeaways
  • A pointer stores the address of a variable; it 'points to' it.
  • & gives the address; * gives the value at an address.
  • Each concept has its own program: & and *, change via pointer, arrays, swap, NULL.
  • Array name = address of the first element; *(p+i) walks the array.
  • Call by reference uses & and * so a function can change original variables.
  • Swap is the classic call-by-reference example.
  • Always check for NULL before using a pointer.
  • (Advanced pointer topics — pointers to functions, linked lists — are beyond beginner scope.)

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1