Simple 1-dimension arrays are considered as a pointer, but is it also the case for a matrix ?
However, a cube int[5][5][5]
would also be considered as an int ***
?
Simple 1-dimension arrays are considered as a pointer, but is it also the case for a matrix ?
However, a cube int[5][5][5]
would also be considered as an int ***
?
No, a pointer to a pointer to an integer is not the same as an array of arrays of integers.
Think about how they would look in memory.
Array of arrays (e.g. int a[2][2]
):
+---------+---------+---------+---------+ | a[0][0] | a[0][1] | a[1][0] | a[1][1] | +---------+---------+---------+---------+
Pointer to pointer (e.g. int **a
):
+------+------+------+ | a[0] | a[1] | .... | +------+------+------+ | | | v | +---------+---------+---------+ | | A[1][0] | a[1][1] | ....... | | +---------+---------+---------+ v +---------+---------+---------+ | A[0][0] | a[0][1] | ....... | +---------+---------+---------+
No they are not. For example consider the two declarations given below.
int c[5][5][5];
int ***d;
For the first declaration the memory requirement is 125 X 4 = 500 Bytes.
Where as for the second declaration the memory requirement is just 4 Bytes. For many practical purposes we can interchange between an array and a pointer but they are not the same.
Obviously Not. An array of array of integers are different from pointer to pointer to integer. Array is a collection of similar data elements stored in contiguous memory location. But pointer to pointer to integers are not.
int a[5][5];
In this declaration, it will allocate contiguous memory location for all elements. say starting memory location is 1000.
array starting array elements
address
a[0]--> 1000 --> a[0][0] a[0][1] ... a[0][4]
a[1]--> 1020 --> a[1][0] a[1][1] ... a[1][4]
a[2]--> 1040 --> a[2][0] a[2][1] ... a[2][4]
a[3]--> 1060 --> a[3][0] a[3][1] ... a[3][4]
a[4]--> 1080 --> a[4][0] a[4][1] ... a[4][4]
But when you have pointer to pointer to integer like this-
int **a;
when you allocate memory for this-
array --> a[0] a[1] a[2] a[3] a[4]
address > 1000 1004 1008 1012 1016 // address of array which points to some other location
This will be allocated contiguous memory locations. but each array will point/ holds some other memory location for elements.
array's array element's array elements
address address
1000 --> a[0]--> 2000 --> a[0][0] a[0][1] ... a[0][4]
1004 --> a[1]--> 3000 --> a[1][0] a[1][1] ... a[1][4]
1008 --> a[2]--> 4000 --> a[2][0] a[2][1] ... a[2][4]
1012 --> a[3]--> 5000 --> a[3][0] a[3][1] ... a[3][4]
1016 --> a[4]--> 6000 --> a[4][0] a[4][1] ... a[4][4]
These arrays holds the starting address of their elements in different locations.