Pointers and Arrays in C
Arrays and pointers are close friends in C. The array name itself works like a pointer to the first element, and the familiar a[i] is really pointer arithmetic in disguise.
In this lesson you will see this relationship clearly, learn where arrays and pointers are different (a favourite interview question), and access 1D and 2D arrays with pointers.
What You Will Learn
- Why an array name gives the base address.
- Why
a[i]is the same as*(a + i). - Four ways to access array elements.
- The differences between an array and a pointer.
- How to access a 2D array with pointers.
Relationship Between Arrays and Pointers in C
Exam definition: In most expressions, the name of an array is converted to a pointer to its first element (a becomes &a[0]). The subscript a[i] is defined as *(a + i): move i elements from the base address and take the value there.
Memory trick: The array name is the gate number of a housing society: it tells where house 0 starts. a[i] = "from the gate, walk i houses, then go inside" = *(a + i).
int a[4] = {10, 20, 30, 40}; int *p = a;
a[0] a[1] a[2] a[3]
+------+------+------+------+
| 10 | 20 | 30 | 40 |
+------+------+------+------+
^ ^
a a+1 ... a == &a[0]
p p+1 a + i == &a[i]
*(a+i)== a[i] == p[i] == *(p+i)
Syntax: Four Ways to Access an Element
int a[5] = {...};
int *p = a; /* same as p = &a[0]; no & needed */
a[i] /* subscript on the array */
*(a + i) /* pointer arithmetic on the array name */
p[i] /* subscript on the pointer */
*(p + i) /* pointer arithmetic on the pointer */
All four give the same element. Fun fact: since a[i] is *(a + i) and addition can swap sides, i[a] also works. Never write it in real code, but it's a popular interview puzzle.
Example 1: Same Element, Four Ways
#include <stdio.h>
int main(void)
{
int a[5] = {11, 22, 33, 44, 55};
int *p = a;
int i;
printf("i a[i] *(a+i) p[i] *(p+i)\n");
for (i = 0; i < 5; i++)
{
printf("%d %4d %6d %4d %6d\n", i, a[i], *(a + i), p[i], *(p + i));
}
printf("a == &a[0] ? %s\n", a == &a[0] ? "yes" : "no");
printf("2[a] = %d (same as a[2])\n", 2[a]);
return 0;
}
Output
i a[i] *(a+i) p[i] *(p+i) 0 11 11 11 11 1 22 22 22 22 2 33 33 33 33 3 44 44 44 44 4 55 55 55 55 a == &a[0] ? yes 2[a] = 33 (same as a[2])
Code Explanation
int *p = a;works without&becauseaalready becomes the address ofa[0].- Each row prints the same element four ways. The compiler turns
a[i]into*(a + i)anyway. a == &a[0]confirms that the array name is the base address.2[a]is*(2 + a)=*(a + 2)= 33. It proves the subscript is just pointer arithmetic.
Difference Between an Array and a Pointer
An array name behaves like a pointer, but it is not a pointer variable:
| Point | Array int a[5] | Pointer int *p = a |
|---|---|---|
| What it is | A block of 5 ints | One variable holding an address |
sizeof | 20 (whole array) | 8 (just the pointer, 64-bit) |
Can it change? (a++ / p++) | No: a is not assignable | Yes: p can move |
| Can it point elsewhere? | No, always the same memory | Yes: p = &x; |
&a / &p | Address of whole array (same value as a) | Address of the pointer variable |
| Memory | Allocated for all elements | Only for the address |
Example 2: sizeof, a Moving Pointer, and a 2D Array
#include <stdio.h>
int main(void)
{
int a[5] = {2, 4, 6, 8, 10};
int *p = a;
int m[2][3] = { {1, 2, 3}, {4, 5, 6} };
int (*row)[3] = m; /* pointer to an array of 3 ints */
int i, j, sum = 0;
printf("sizeof(a) = %zu, elements = %zu\n", sizeof(a), sizeof(a) / sizeof(a[0]));
while (p < a + 5) /* p moves, a stays fixed */
sum += *p++;
printf("sum using moving pointer = %d\n", sum);
printf("2D using pointers:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
printf("%d ", *(*(row + i) + j)); /* same as m[i][j] */
printf("\n");
}
return 0;
}
Output
sizeof(a) = 20, elements = 5 sum using moving pointer = 30 2D using pointers: 1 2 3 4 5 6
sum += *p++;reads the value and then moves p. You could not writea++; the array name is fixed.int (*row)[3]is a pointer to a whole row of 3 ints. The brackets around*rowmatter:int *row[3]would be an array of 3 pointers (a later lesson).row + ijumps i whole rows.*(row + i)is that row (an array, so it becomes a pointer to its first int), and+ jthen moves to column j.- Rule:
m[i][j] == *(*(m + i) + j). This formula is asked in exams.
Important Points to Remember
- The array name gives the base address:
a == &a[0]. - a[i] == *(a + i) == p[i] == *(p + i).
- An array name is not assignable:
a++anda = pare errors. - sizeof(array) gives the whole array; sizeof(pointer) gives 4 or 8.
- 2D: m[i][j] == *(*(m + i) + j).
int (*p)[3]= pointer to array;int *p[3]= array of pointers.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
a++ on an array name | Compile error | Use a pointer: p++ |
int *p = &a; for int a[5] | Type warning (&a is pointer to whole array) | int *p = a; |
*a + i instead of *(a + i) | Gives a[0] + i | Keep the brackets |
int *row[3] = m; for a 2D array | Wrong type: array of pointers | int (*row)[3] = m; |
| Expecting sizeof(p) to give array size | Gives 8 | Keep the count separately |
Pointers and Arrays Practice Questions
- Write
a[3]using pointer notation. - Why is
a++invalid for an array butp++valid? - For
int a[10]andint *p = a, what aresizeof(a)andsizeof(p)on a 64-bit system? - Write
m[1][2]using only pointer notation. - What is the difference between
int (*p)[4]andint *p[4]?
Show answers
*(a + 3).- An array name is not a variable that can be changed; a pointer is a variable.
- 40 and 8.
*(*(m + 1) + 2).- The first is one pointer to an array of 4 ints; the second is an array of 4 int pointers.
Coding Exercise
Write a function void reverse(int *start, int *end) that reverses an array using two pointers moving toward each other. Test with {1, 2, 3, 4, 5, 6}.
Show solution
#include <stdio.h>
void reverse(int *start, int *end)
{
int temp;
while (start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5, 6};
int *p;
reverse(a, a + 5); /* first and last element */
for (p = a; p < a + 6; p++)
printf("%d ", *p);
printf("\n");
return 0;
}
6 5 4 3 2 1
Quick Quiz: Pointers and Arrays (MCQ)
-
a[i]is equivalent to:A)
*a + iB)*(a + i)C)&a + iD)a + *i -
For
int a[5], which is an error?A)
int *p = a;B)*(a + 1) = 3;C)a++;D)p = a + 2; -
int a[] = {7, 8, 9}; printf("%d", *(a + 2));prints:A) 7 B) 8 C) 9 D) Address
-
m[i][j]equals:A)
*(m + i + j)B)*(*(m + i) + j)C)**m + i + jD)*m[i + j] -
int (*p)[3];declares:A) An array of 3 pointers B) A pointer to an array of 3 ints C) A function D) A 3D array
Show answers
- B:
*(a + i). - C:
a++. - C: 9.
- B:
*(*(m + i) + j). - B: a pointer to an array of 3 ints.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Array name | Base address: a == &a[0] |
|---|---|
| a[i] | *(a + i) |
| m[i][j] | *(*(m + i) + j) |
| a++ | Error: array name not assignable |
| sizeof(a) vs sizeof(p) | Whole array vs 4/8 bytes |
| int (*p)[3] | Pointer to array of 3 ints |
Frequently asked questions
- What is the relationship between arrays and pointers?
- The array name decays to a pointer to its first element, and a[i] means *(a + i).
- What is the difference between an array and a pointer?
- An array is a fixed block of elements (sizeof = whole size, can't move); a pointer is a variable holding an address (can move).
- Why does i[a] work?
- Because a[i] is *(a + i) and addition is commutative, so i[a] is *(i + a).
Remember
- Array name = gate number; a[i] = walk i houses, go inside.
- Array stays, pointer moves.
Next Topic
Next lesson: Pointers and Strings in C. You will handle strings with char pointers and learn why some strings can't be changed.