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
Intuition
An array is a street of houses; a pointer is a piece of paper with a house address written on it. When you mention the street’s name in a sentence, C usually replaces it by the address of its first house. But the street is still a street: it has a length, and you cannot move it to another place.
Definition 2.5 · Array-to-pointer conversion (decay)
In almost every expression, the name of an array is automatically converted to a pointer to its first element: a becomes &a[0]. This array decay does not happen when the array is the operand of sizeof or of &, or when a string literal initialises an array.
So, after int a[10]; int *x;, the assignment x = a; stores the address of a[0] in x.
Correction · An array name is not a pointer
Study material says: §2.2 says “an array name without an index is a pointer to the first element in the array”, and that p and &p[0] are identical.
Correct version: An array name names the whole array. In most expressions it is converted to a pointer to the first element, so the value of p there equals &p[0]. But it is not a pointer variable: sizeof p gives the size of the whole array, &p has the type “pointer to the whole array”, and you cannot assign to p.
Why: C17 standard §6.3.2.1 paragraph 3 lists the exceptions (sizeof, unary &, string-literal initialiser). The program below shows sizeof a = 40 bytes in main but only 8 (a pointer) inside a function, and &a + 1 moving 40 bytes while a + 1 moves 4.
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). a [ i ] ≡ ∗ ( a + i ) .
So x[5] = 100; and *(x + 5) = 100; do exactly the same thing: they store 100 in the sixth element.
Example 2.6 Arrays, pointers and sizeof
Goal. Show that x[5], *(a + 5) and a[5] are the same element, and that the array name keeps its full size only where no conversion happens.
Program.
C · array_pointer.c Copy #include <stdio.h>
/* An array parameter is really a pointer: "int arr[]" in a parameter
list means exactly "int *arr", so sizeof gives the pointer size. */
void show_size( int * arr)
{
printf( "inside function: sizeof arr = %zu (size of a pointer) \n " , sizeof arr);
}
int main( void )
{
int a[ 10 ] = { 0 };
int * x = a; /* a is converted to &a[0] */
x[ 5 ] = 100 ; /* same as *(x + 5) = 100 */
printf( "a[5] = %d, *(a + 5) = %d, 5[a] = %d \n " , a[ 5 ], * (a + 5 ), 5 [a]);
printf( "x == &a[0]: %s \n " , x == & a[ 0 ] ? "yes" : "no" );
printf( "in main: sizeof a = %zu (whole array) \n " , sizeof a);
show_size(a);
/* &a and a give the same address but different types:
a + 1 moves one int, &a + 1 moves the whole array. */
printf( "(char *)(a + 1) - (char *)a = %td bytes \n " ,
( char * )(a + 1 ) - ( char * )a);
printf( "(char *)(&a + 1) - (char *)&a = %td bytes \n " ,
( char * )( & a + 1 ) - ( char * ) & a);
return 0 ;
}
Run it.
Output (sizes may differ on other systems) Copy a[5] = 100, *(a + 5) = 100, 5[a] = 100
x == &a[0]: yes
in main: sizeof a = 40 (whole array)
inside function: sizeof arr = 8 (size of a pointer)
(char *)(a + 1) - (char *)a = 4 bytes
(char *)(&a + 1) - (char *)&a = 40 bytes
Read the result. All three forms reach the same element (even the odd-looking 5[a], because it means *(5 + a)). In main, sizeof a is 40 bytes; in the function the parameter is a pointer, so its size is 8. a + 1 advances one int (4 bytes) but &a + 1 advances one whole array (40 bytes).
Result. a[i] means *(a + i); an array passed to a function arrives as a pointer, so pass its length as a separate argument.
Two-dimensional arrays and pointers
A 2-D array int a[R][C] is stored as R R R rows one after another, each row holding C C C elements (the row length). To reach a[j][k], skip j j j full rows (j × C j \times C j × C elements) and then k k k more elements:
a [ j ] [ k ] ≡ ∗ ( ( int ∗ ) a + j × C + k ) . a[j][k] \;\equiv\; *\big((\text{int } *)a + j \times C + k\big). a [ j ] [ k ] ≡ ∗ ( ( int ∗ ) a + j × C + k ) .
For example, a[0][4] is *((int *)a + 4).
Example 2.7 Real offset of a[j][k] versus the two formulas
Goal. For int a[3][5] (row length 5), measure where each element really lies and compare with j × 5 + k j \times 5 + k j × 5 + k and ( j × 5 ) × k (j \times 5) \times k ( j × 5 ) × k .
Program. The distance in bytes from the start of the array, divided by sizeof(int), gives the real offset in elements.
C · two_d_pointer.c Copy #include <stdio.h>
#define ROWS 3
#define COLS 5 /* the row length */
int main( void )
{
int a[ROWS][COLS];
for ( int j = 0 ; j < ROWS; j ++ ) /* fill: a[j][k] = 10*j + k */
for ( int k = 0 ; k < COLS; k ++ )
a[j][k] = 10 * j + k;
printf( "a[0][4] = %d, *((int *)a + 4) = %d \n " , a[ 0 ][ 4 ], * (( int * )a + 4 ));
printf( "a[2][3] = %d, *(*(a + 2) + 3) = %d \n " , a[ 2 ][ 3 ], * ( * (a + 2 ) + 3 ));
/* Where is a[j][k] really stored? Measure its distance from the start
of the array in bytes and divide by sizeof(int) to get elements. */
printf( "j k real offset j*COLS+k (j*COLS)*k \n " );
for ( int j = 0 ; j < ROWS; j ++ )
for ( int k = 1 ; k < COLS; k += 2 ) {
long real = ( long )((( char * ) & a[j][k] - ( char * )a) / sizeof ( int ));
printf( "%d %d %11ld %8d %10d \n " , j, k, real, j * COLS + k, (j * COLS) * k);
}
return 0 ;
}
Run it.
Output Copy a[0][4] = 4, *((int *)a + 4) = 4
a[2][3] = 23, *(*(a + 2) + 3) = 23
j k real offset j*COLS+k (j*COLS)*k
0 1 1 1 0
0 3 3 3 0
1 1 6 6 5
1 3 8 8 15
2 1 11 11 10
2 3 13 13 30
Result. The real offset always equals j × rowlength + k j \times \text{rowlength} + k j × rowlength + k ; the multiplied version is wrong for every row except by accident.
Key idea
a[i] is defined as *(a + i), and an array name converts to a pointer to its first element in most expressions, but it is not itself a pointer: sizeof still sees the whole array.
TEE Exam note
For “relation between arrays and pointers” write: the array name converts to &a[0]; a[i] ≡ *(a + i); pointer arithmetic moves in units of the element size; a pointer can be subscripted like an array; give the 2-D formula *((int *)a + j * C + k) with a numeric example. Mention that an array parameter in a function is a pointer, so its length must be passed separately.
Common mistake
Using sizeof arr / sizeof arr[0] inside a function to find the length of an array parameter. There arr is a pointer, so the result is wrong (for example 8 / 4 = 2). Pass the length as a parameter.
Common mistake
Assigning to an array name, e.g. a = b; or a++. An array cannot be assigned; only a pointer variable can be changed. Copy element by element (or use memcpy).
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 i i i elements, not i i i bytes.
In a function parameter, int arr[] means int *arr.
2-D element: a[j][k] ≡ *((int *)a + j * C + k) where C C C 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]?
A x[3]B *(a + 3)C *a + 3D *(x + 3)
Check answer
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];?
A 4 B 8 C 10 D 40
Check answer
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]?
A *((int *)a + 2 * 5 * 6)B *((int *)a + 2 * 6 + 5)C *((int *)a + 2 * 4 + 5)D *((int *)a + (2 * 6) * 5)
Check answer
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 Copy #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 ;
} A 10 30 0 B 20 40 10 C 20 30 10 D 11 12 10
Check answer
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?
A Because sizeof does not work on arrays B Because the function receives a copy of only the first element C Because an array parameter is really a pointer, so sizeof gives the pointer size D Because arrays cannot be passed to functions
Check answer
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 i i i 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).