MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 3 of 8

Block 1 · Unit 2Arrays, Pointers and Structures12 min · Section 3 of 8

2.3Arrays and Pointers

Covers IGNOU MCS-208 §2.2

In C, arrays and pointers are closely connected: subscripting is defined through pointer arithmetic. Understanding this connection explains how arrays are passed to functions and how a 2-D element is located in memory.

Array names and pointers

So, after int a[10]; int *x;, the assignment x = a; stores the address of a[0] in x.

Subscripts are pointer arithmetic

If p points to an element of an array, p + i points i elements further on (not i bytes): the compiler multiplies i by the element size. The C standard defines subscripting this way:

a[i]  ≡  ∗(a+i).a[i] \;\equiv\; *(a + i).

So x[5] = 100; and *(x + 5) = 100; do exactly the same thing: they store 100 in the sixth element.

Two-dimensional arrays and pointers

A 2-D array int a[R][C] is stored as RR rows one after another, each row holding CC elements (the row length). To reach a[j][k], skip jj full rows (j×Cj \times C elements) and then kk more elements:

a[j][k]  ≡  ∗((int ∗)a+j×C+k).a[j][k] \;\equiv\; *\big((\text{int } *)a + j \times C + k\big).

For example, a[0][4] is *((int *)a + 4).

Key takeaways

  • An array name decays to a pointer to its first element, except with sizeof, & and string-literal initialisation.
  • a[i] ≡ *(a + i); p + i moves ii elements, not ii bytes.
  • In a function parameter, int arr[] means int *arr.
  • 2-D element: a[j][k] ≡ *((int *)a + j * C + k) where CC is the row length.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

After int a[10], *x = a;, which expression does not refer to the element a[3]?

Show answer

Answer: C. *a + 3

*a + 3 reads a[0] and adds 3 to its value; the parentheses matter. The other three all mean *(a + 3).

Question 2 · multiple choice

With 4-byte int and 8-byte pointers, what does sizeof a give inside main for int a[10];?

Show answer

Answer: D. 40

sizeof is one of the exceptions where the array does not decay, so it measures all 10 elements. 8 would be the size inside a function that receives a as a parameter.

Question 3 · multiple choice

For int a[4][6];, which expression is equivalent to a[2][5]?

Show answer

Answer: B. *((int *)a + 2 * 6 + 5)

Skip 2 rows of length 6 (12 elements) and add 5: offset 17. Using 4 (the number of rows) instead of the row length 6 is the most common slip.

Question 4 · multiple choice

What does this program print?

C · q2-ptr.c
#include <stdio.h>

int main(void)
{
    int a[] = {10, 20, 30, 40};
    int *p = a + 1;
    printf("%d %d %d\n", *p, p[2], *(p - 1));
    return 0;
}
Show answer

Answer: B. 20 40 10

p points to a[1] (20); p[2] is *(p + 2) = a[3] (40); *(p - 1) is a[0] (10). “20 30 10” forgets that p[2] counts from p, not from a.

Question 5 · multiple choice

Why does printf("%zu", sizeof arr) inside void f(int arr[]) not print the size of the caller’s array?

Show answer

Answer: C. Because an array parameter is really a pointer, so sizeof gives the pointer size

int arr[] in a parameter list means int *arr. The function gets the address of the first element, not the array or a copy of it.

Question 6 · short answer

Explain the relation between arrays and pointers in C with an example.

Show worked answer

In most expressions an array name is converted to a pointer to its first element, so after int a[10], *x; x = a; the pointer x holds &a[0]. Subscripting is defined through pointer arithmetic: a[i] is *(a + i), where a + i points ii elements (not bytes) after a[0]. So x[5] = 100; and *(x + 5) = 100; both store 100 in a[5]. The array name is not a pointer variable: it cannot be assigned, and sizeof a gives the whole array size (40 bytes for 10 four-byte int). For a 2-D array int a[R][C], a[j][k] ≡ *((int *)a + j * C + k).

↑ ↓ move↵ openTab switch type