Arrays of Arrays Introduction
In this diagram, a is an array of 5 characters. Each element is a char and is 1 byte in size.char a[5] = {'a', 'b', 'c', 'd', 'e'};
int a[5] = {1, 2, 3, 4, 5};
double a[4] = {1.1, 2.2, 3.3, 4.4};
struct Time { int hours; int minutes; int seconds; } struct Time d[3] = { {3, 30, 0}, {9, 45, 0}, {10, 15, 0} };
We typically draw the array of arrays like this, which models what is being represented such as a 2D x/y grid, a matrix, or a table of some sort. Anything that might have rows and columns is a good candidate for this type of structure.![]()
You access each character using two subscripts: e[row][column] where row and column are integers between 0 and 4. For example, the letter z would be accessed as e[1][4].![]()
Arrays and Pointers Declaration Basics
What is the English equivalent of each declaration? In other words, what is the type of f1, f2, etc?
This is how to interpret the shapes used below. The diagrams assume 4-byte integers and 8-byte pointers.- int f1;
- int *f2;
- int f3[3];
- int *f4[3];
- int (*f5)[3];
- int *(*f6)[3];
- int f7[3][4];
- int *f8[3][4];
- int (*f9)[3][4];
- int *(*f10)[3][4];
- int (*f11[3])[4];
- int *(*f12[3])[4];
The shaded shape is the variable itself, i.e., not what's being pointed to. This should help you answer the sizeof questions. Also, the thick black lines indicate what is being pointed at, e.g. a single int/pointer or an array on ints/pointers.
pointer int ![]()
![]()
Oftentimes, the pointer is actually pointing at an integer inside of an array. So this code:
would look like this, with the pointer pointing at the first element of the array and NOT the entire array:int a[3]; int *f2 = a; /* same as &a[0] */
and this code:
would look like this:f2 = &a[1];
This is simply because f2 is a pointer to an int, NOT a pointer to an array of ints.
/* f5 points to the entire array */
/* f6 points to the entire array */
/* f9 points to the entire array */
Showing it with all of the pointers:
/* f10 points to the entire array */
The blue box is f11:
The blue box is *f11:
The blue box is **f11:
The blue box is ***f11:
/* each element of f11 points to an entire array */
Showing another view with all of the pointers. Following the arrow from f12
It doesn't matter how you draw the arrays, either vertically or horizontally, as long as you understand the diagrams.