Introduction & Syntax
Arrays · Study material
An array in C is a fixed-size block of elements of the same type, stored one after another in memory and reached by an index that starts at 0. Arrays hold marks, sensor readings, matrices and buffers, and they are behind strings, sorting and searching. They also hide some of C's most-asked traps: decay to pointers, sizeof inside functions, and reading past the end. This page covers arrays from the basics to modern C (C99 to C23), then gives you 40 practice questions whose answers were checked on a real compiler.
On this page
- Declaring and initialising arrays, including designated initializers
- Array decay,
sizeofand passing arrays to functions - 2D arrays, variable length arrays and compound literals
- What is new in C23:
= {},constexprsizes andtypeof
Declaring and initialising an array
Write the type, the name and the number of elements: int marks[5];. Elements are marks[0] to marks[4]. A local array without an initializer holds garbage; with an initializer, every element you do not list becomes 0.
| Declaration | Result |
|---|---|
int a[5] = {1, 2}; | 1 2 0 0 0 |
int a[5] = {0}; | all zero (works in every C version) |
int a[] = {7, 8, 9}; | size taken from the list: 3 elements |
int a[6] = {[1] = 72, [4] = 95}; | designated initializers (C99): 0 72 0 0 95 0 |
int a[5] = {}; | C23: all zero |
#include <stdio.h>
#define COUNT(a) (sizeof (a) / sizeof (a)[0]) /* element count – only for real arrays */
int main(void) {
int marks[6] = {[1] = 72, [4] = 95}; /* designated initializers: the rest become 0 */
for (size_t i = 0; i < COUNT(marks); i++)
printf("%d ", marks[i]);
printf("\nelements: %zu\n", COUNT(marks));
return 0;
}
0 72 0 0 95 0 elements: 6
Array decay and sizeof
In almost every expression an array name turns into a pointer to its first element – it decays. There are three exceptions: the operand of sizeof, the operand of unary &, and a string literal used to initialise a char array. That is why sizeof a gives the whole array in main() but only the pointer size inside a function, and why &a + 1 jumps over the whole array while a + 1 moves one element.
#include <stdio.h>
void show(int *a) {
printf("in show(): sizeof a = %zu\n", sizeof a); /* only a pointer arrived */
}
int main(void) {
int a[10] = {0};
printf("in main(): sizeof a = %zu\n", sizeof a); /* the whole array */
show(a); /* a decays to &a[0] */
printf("a + 1 moves %td bytes, &a + 1 moves %td bytes\n",
(char *) (a + 1) - (char *) a, (char *) (&a + 1) - (char *) a);
return 0;
}
in main(): sizeof a = 40 in show(): sizeof a = 8 a + 1 moves 4 bytes, &a + 1 moves 40 bytes
Always pass the length. A function receiving int a[] really receives int *a, so sizeof a / sizeof a[0] inside it is wrong. Use a signature such as void sort(int *a, size_t n).
C99 also lets a parameter promise a minimum size: void f(int a[static 10]) tells the compiler (and the reader) that the caller passes at least 10 elements.
Two-dimensional arrays
int m[3][4] is an array of 3 rows, each an array of 4 ints, stored row by row (row-major). The address of m[i][j] is the base plus (i × 4 + j) × sizeof(int). When you pass a 2D array to a function, every size except the first must be given: void f(int m[][4], int rows).
#include <stdio.h>
int main(void) {
int m[3][4];
for (int r = 0; r < 3; r++)
for (int c = 0; c < 4; c++)
m[r][c] = r * 10 + c;
printf("m[1][2] = %d\n", m[1][2]);
printf("*(*(m + 2) + 3) = %d\n", *(*(m + 2) + 3)); /* same as m[2][3] */
printf("rows = %zu, columns = %zu\n", sizeof m / sizeof m[0], sizeof m[0] / sizeof m[0][0]);
return 0;
}
m[1][2] = 12 *(*(m + 2) + 3) = 23 rows = 3, columns = 4
Compound literals and variable length arrays
A compound literal creates an unnamed array right where you need it – handy for passing test data to a function.
#include <stdio.h>
int sum(const int *a, size_t n) {
int s = 0;
for (size_t i = 0; i < n; i++)
s += a[i];
return s;
}
int main(void) {
/* A compound literal is an unnamed array created on the spot (C99) */
printf("%d\n", sum((int[]){4, 8, 15, 16, 23, 42}, 6));
return 0;
}
108
A variable length array (VLA) gets its size at run time. VLAs are mandatory in C99 and optional since C11, live on the stack, and cannot be given an initializer list. They are most useful as parameter types, so a function can take a 2D array of any size.
#include <stdio.h>
void fill(int rows, int cols, int t[rows][cols]) { /* the sizes come first */
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
t[r][c] = (r + 1) * (c + 1);
}
int main(void) {
int n = 3;
int t[n][n]; /* variable length array: size known only at run time */
fill(n, n, t);
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++)
printf("%3d", t[r][c]);
printf("\n");
}
return 0;
}
1 2 3 2 4 6 3 6 9
What C23 adds
C23 is the newest C standard (GCC 13+ and Clang 17+ support most of it). For arrays it brings the empty initializer = {}, constexpr constants that can size an array, and typeof to declare a variable with another variable's type.
#include <stdio.h>
int main(void) {
constexpr int N = 4; /* C23: a true compile-time constant */
int a[N] = {}; /* C23: empty initializer, every element is 0 */
typeof(a) b = {1, 2, 3, 4}; /* C23: b gets the same type as a, int[4] */
a[N - 1] = b[0] + b[N - 1];
printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
return 0;
}
0 0 0 5
Common array mistakes
- Off-by-one:
for (i = 0; i <= n; i++)touchesa[n], one past the end. - Assigning arrays:
b = a;does not compile; copy with a loop ormemcpy(). - Returning a local array: its memory is gone when the function returns.
- Counting with sizeof on a parameter: it measures a pointer, not the array.
- memset with a value other than 0: it sets bytes, not ints.
Key points to remember
- Indexes run from 0 to n − 1; C never checks the bounds for you.
a[i]is*(a + i), so eveni[a]is legal.- An array decays to a pointer except with
sizeof,&and string-literal initialisation. - Arrays inside a
structare copied when the struct is assigned. - Pass the length with the array, and mark read-only arrays
const.
Frequently asked questions
Why do C arrays start at index 0?
Because the index is an offset from the start: a[0] is the element 0 places after the base address.
How do I find the length of an array in C?
Use sizeof a / sizeof a[0] where the array itself is in scope. Inside a function that received the array, pass the length as a separate argument.
Can the size of an array change?
No. For a size decided at run time use a VLA (fixed once created) or malloc(), and grow heap memory with realloc().
Where can I learn arrays step by step?
Read Arrays in C, 2D Arrays and Matrices and Searching and Sorting, then practise with the exercises below.