I tried to assign two fixed-size arrays to an array of pointers to them, but the compiler warns me and I don\'t understand why.
int A[5][5];
int B[5][5];
int
A common misconception among C beginners is that they just assume pointers and arrays are equivalent. That's completely wrong.
Confusion comes to beginners when they see the code like
int a1[] = {1,2,3,4,5};
int *p1 = a1; // Beginners intuition: If 'p1' is a pointer and 'a1' can be assigned
// to it then arrays are pointers and pointers are arrays.
p1[1] = 0; // Oh! I was right
a1[3] = 0; // Bruce Wayne is the Batman! Yeah.
Now, it is verified by the beginners that arrays are pointers and pointers are arrays so they do such experiments:
int a2[][5] = {{0}};
int **p2 = a2;
And then a warning pops up about incompatible pointer assignment then they think: "Oh my God! Why has this array become Harvey Dent?".
Some even goes to one step ahead
int a3[][5][10] = {{{0}}};
int ***p3 = a3; // "?"
and then Riddler comes to their nightmare of array-pointer equivalence.
Always remember that arrays are not pointers and vice-versa. An array is a data type and a pointer is another data type (which is not array type). This has been addressed several years ago in the C-FAQ:
Saying that arrays and pointers are "equivalent" means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it's "pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.")
Now always remember few important rules for array to avoid this kind of confusion:
sizeof
and &
operator.Now you have the rules, you can conclude that in
int a1[] = {1,2,3,4,5};
int *p1 = a1;
a1
is an array and in the declaration int *p1 = a1;
it converted to pointer to its first element. Its elements are of type int
then pointer to its first element would be of type int *
which is compatible to p1
.
In
int a2[][5] = {{0}};
int **p2 = a2;
a2
is an array and in int **p2 = a2;
it decays to pointer to its first element. Its elements are of type int[5]
(a 2D array is an array of 1D arrays), so a pointer to its first element would be of type int(*)[5]
(pointer to array) which is incompatible with type int **
. It should be
int (*p2)[5] = a2;
Similarly for
int a3[][5][10] = {{{0}}};
int ***p3 = a3;
elements of a3
is of type int [5][10]
and pointer to its first element would be of type int (*)[5][10]
, but p3
is of int ***
type, so to make them compatible, it should be
int (*p3)[5][10] = a3;
Now coming to your snippet
int A[5][5];
int B[5][5];
int*** C = {&A, &B};
&A
and &B
are of type int(*)[5][5]
. C
is of type int***
, it's not an array. Since you want to make C
to hold the address of both the arrays A
and B
, you need to declare C
as an array of two int(*)[5][5]
type elements. This should be done as
int (*C[2])[5][5] = {&A, &B};
However, if I dynamically allocate A and B it works just fine. Why is this?
In that case you must have declared A
and B
as int **
. In this case both are pointers, not arrays. C
is of type int ***
, so it can hold an address of int**
type data. Note that in this case the declaration int*** C = {&A, &B};
should be
int*** C = &A;
In case of int*** C = {&A, &B};
, the behavior of program would be either undefined or implementation defined.
C11: 5.1.1.3 (P1):
A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined
Read this post for further explanation.
Arrays are not the same thing as multi-dimensional pointers in C. The name of the array gets interpreted as the address of the buffer that contains it in most cases, regardless of how you index it. If A
is declared as int A[5][5]
, then A
will usually mean the address of the first element, i.e., it is interpreted effectively as an int *
(actually int *[5]
), not an int **
at all. The computation of the address just happens to require two elements: A[x][y] = A + x + 5 * y
. This is a convenience for doing A[x + 5 * y]
, it does not promote A
to multidimensional buffer.
If you want multi-dimensional pointers in C, you can do that too. The syntax would be very similar, but it requires a bit more set up. There are a couple of common ways of doing it.
With a single buffer:
int **A = malloc(5 * sizeof(int *));
A[0] = malloc(5 * 5 * sizeof(int));
int i;
for(i = 1; i < 5; i++) {
A[i] = A[0] + 5 * i;
}
With a separate buffer for each row:
int **A = malloc(5 * sizeof(int *));
int i;
for(i = 0; i < 5; i++) {
A[i] = malloc(5 * sizeof(int));
}
I am a great believer in using typedef
:
#define SIZE 5
typedef int OneD[SIZE]; // OneD is a one-dimensional array of ints
typedef OneD TwoD[SIZE]; // TwoD is a one-dimensional array of OneD's
// So it's a two-dimensional array of ints!
TwoD a;
TwoD b;
TwoD *c[] = { &a, &b, 0 }; // c is a one-dimensional array of pointers to TwoD's
// That does NOT make it a three-dimensional array!
int main() {
for (int i = 0; c[i] != 0; ++i) { // Test contents of c to not go too far!
for (int j = 0; j < SIZE; ++j) {
for (int k = 0; k < SIZE; ++k) {
// c[i][j][k] = 0; // Error! This proves it's not a 3D array!
(*c[i])[j][k] = 0; // You need to dereference the entry in c first
} // for
} // for
} // for
return 0;
} // main()
If you want a declaration of C
that fits the existing declarations of A
and B
you need to do it like this:
int A[5][5];
int B[5][5];
int (*C[])[5][5] = {&A, &B};
The type of C
is read as "C
is an array of pointers to int [5][5]
arrays". Since you can't assign an entire array, you need to assign a pointer to the array.
With this declaration, (*C[0])[1][2]
is accessing the same memory location as A[1][2]
.
If you want cleaner syntax like C[0][1][2]
, then you would need to do what others have stated and allocate the memory dynamically:
int **A;
int **B;
// allocate memory for A and each A[i]
// allocate memory for B and each B[i]
int **C[] = {A, B};
You could also do this using the syntax suggested by Vlad from Moscow:
int A[5][5];
int B[5][5];
int (*C[])[5] = {A, B};
This declaration of C
is read as "C
is an array of pointers to int [5]
arrays". In this case, each array element of C
is of type int (*)[5]
, and array of type int [5][5]
can decay to this type.
Now, you can use C[0][1][2]
to access the same memory location as A[1][2]
.
This logic can be expanded to higher dimensions as well:
int A[5][5][3];
int B[5][5][3];
int (*C[])[5][3] = {A, B};
Unfortunately there's a lot of crappy books/tutorials/teachers out there who will teach you wrong things....
Forget about pointer-to-pointers, they have nothing to do with arrays. Period.
Also as a rule of thumb: whenever you find yourself using more than 2 levels of indirection, it most likely means that your program design is fundamentally flawed and needs to be remade from scratch.
To do this correctly, you would have to do like this:
A pointer to an array int [5][5]
is called array pointer and is declared as int(*)[5][5]
. Example:
int A[5][5];
int (*ptr)[5][5] = &A;
If you want an array of array pointers, it would be type int(*[])[5][5]
. Example:
int A[5][5];
int B[5][5];
int (*arr[2])[5][5] = {&A, &B};
As you can tell this code looks needlessly complicated - and it is. It will be a pain to access the individual items, since you will have to type (*arr[x])[y][z]
. Meaning: "in the array of array pointers take array pointer number x, take the contents that it points at - which is a 2D array - then take item of index [y][z] in that array".
Inventing such constructs is just madness and nothing I would recommend. I suppose the code can be simplified by working with a plain array pointer:
int A[5][5];
int B[5][5];
int (*arr[2])[5][5] = {&A, &B};
int (*ptr)[5][5] = arr[0];
...
ptr[x][y][z] = 0;
However, this is still somewhat complicated code. Consider a different design entirely! Examples:
Although arrays and pointers are closely associated, they are not at all the same thing. People are sometimes confused about this because in most contexts, array values decay to pointers, and because array notation can be used in function prototypes to declare parameters that are in fact pointers. Additionally, what many people think of as array indexing notation really performs a combination of pointer arithmetic and dereferencing, so that it works equally well for pointer values and for array values (because array values decay to pointers).
Given the declaration
int A[5][5];
Variable A
designates an array of five arrays of five int
. This decays, where it decays, to a pointer of type int (*)[5]
-- that is, a pointer to an array of 5 int
. A pointer to the whole multi-dimensional array, on the other hand, has type int (*)[5][5]
(pointer to array of 5 arrays of 5 int
), which is altogether different from int ***
(pointer to pointer to pointer to int
). If you want to declare a pointer to a multi-dimensional array such as these then you could do it like this:
int A[5][5];
int B[5][5];
int (*C)[5][5] = &A;
If you want to declare an array of such pointers then you could do this:
int (*D[2])[5][5] = { &A, &B };
Added:
These distinctions come into play in various ways, some of the more important being the contexts where array values do not decays to pointers, and contexts related to those. One of the most significant of these is when a value is the operand of the sizeof
operator. Given the above declarations, all of the following relational expressions evaluate to 1 (true):
sizeof(A) == 5 * 5 * sizeof(int)
sizeof(A[0]) == 5 * sizeof(int)
sizeof(A[0][4]) == sizeof(int)
sizeof(D[1]) == sizeof(C)
sizeof(*C) == sizeof(A)
Additionally, it is likely, but not guaranteed, that these relational expressions evaluate to 1:
sizeof(C) == sizeof(void *)
sizeof(D) == 2 * sizeof(void *)
This is fundamental to how array indexing works, and essential to understand when you are allocating memory.