Is 2d array a double pointer?
No. This line of your program is incorrect:
int **ptr = (int**)matrix;
This answer deals with the same topic
If you want concrete image how multidimensional arrays are implemented:
The rules for multidimensional arrays are not different from those for ordinary arrays, just substitute the "inner" array type as element type. The array items are stored in memory directly after each other:
matrix: 11 22 33 99 44 55 66 110
----------- the first element of matrix
------------ the second element of matrix
Therefore, to address element matrix[x][y], you take the base address of matrix + x*4 + y (4 is the inner array size).
When arrays are passed to functions, they decay to pointers to their first element. As you noticed, this would be int (*)[4]. The 4 in the type would then tell the compiler the size of the inner type, which is why it works. When doing pointer arithmetic on a similar pointer, the compiler adds multiples of the element size, so for matrix_ptr[x][y], you get matrix_ptr + x*4 + y, which is exactly the same as above.
The cast ptr=(int**)matrix is therefore incorrect. For once, *ptr would mean a pointer value stored at address of matrix, but there isn't any. Secondly, There isn't a pointer to matrix[1] anywhere in the memory of the program.
Note: the calculations in this post assume sizeof(int)==1, to avoid unnecessary complexity.
Is 2d array a double pointer?
No. This line of your program is incorrect:
int **ptr = (int**)matrix;
This answer deals with the same topic
If you want concrete image how multidimensional arrays are implemented:
The rules for multidimensional arrays are not different from those for ordinary arrays, just substitute the "inner" array type as element type. The array items are stored in memory directly after each other:
matrix: 11 22 33 99 44 55 66 110
----------- the first element of matrix
------------ the second element of matrix
Therefore, to address element matrix[x][y], you take the base address of matrix + x*4 + y (4 is the inner array size).
When arrays are passed to functions, they decay to pointers to their first element. As you noticed, this would be int (*)[4]. The 4 in the type would then tell the compiler the size of the inner type, which is why it works. When doing pointer arithmetic on a similar pointer, the compiler adds multiples of the element size, so for matrix_ptr[x][y], you get matrix_ptr + x*4 + y, which is exactly the same as above.
The cast ptr=(int**)matrix is therefore incorrect. For once, *ptr would mean a pointer value stored at address of matrix, but there isn't any. Secondly, There isn't a pointer to matrix[1] anywhere in the memory of the program.
Note: the calculations in this post assume sizeof(int)==1, to avoid unnecessary complexity.
No. A multidimensional array is a single block of memory. The size of the block is the product of the dimensions multiplied by the size of the type of the elements, and indexing in each pair of brackets offsets into the array by the product of the dimensions for the remaining dimensions. So..
int arr[5][3][2];
is an array that holds 30 ints. arr[0][0][0] gives the first, arr[1][0][0] gives the seventh (offsets by 3 * 2). arr[0][1][0] gives the third (offsets by 2).
The pointers the array decays to will depend on the level; arr decays to a pointer to a 3x2 int array, arr[0] decays to a pointer to a 2 element int array, and arr[0][0] decays to a pointer to int.
However, you can also have an array of pointers, and treat it as a multidimensional array -- but it requires some extra setup, because you have to set each pointer to its array. Additionally, you lose the information about the sizes of the arrays within the array (sizeof would give the size of the pointer). On the other hand, you gain the ability to have differently sized sub-arrays and to change where the pointers point, which is useful if they need to be resized or rearranged. An array of pointers like this can be indexed like a multidimensional array, even though it's allocated and arranged differently and sizeof won't always behave the same way with it. A statically allocated example of this setup would be:
int *arr[3];
int aa[2] = { 10, 11 },
ab[2] = { 12, 13 },
ac[2] = { 14, 15 };
arr[0] = aa;
arr[1] = ab;
arr[2] = ac;
After the above, arr[1][0] is 12. But instead of giving the int found at 1 * 2 * sizeof(int) bytes past the start address of the array arr, it gives the int found at 0 * sizeof(int) bytes past the address pointed to by arr[1]. Also, sizeof(arr[0]) is equivalent to sizeof(int *) instead of sizeof(int) * 2.
I know that I can go from one of the types 'char [1]' and 'char *' to the other. However, it seems not as easy with 'char [1][1]' and 'char **'.
In my main function I have
`char a[1][1];`
`a[0][0] = 'q';`
`printf("a: %p\n", a);`
`printf("*a: %p\n", *a);`
`printf("**a: %p\n", **a);`I'm of course compiling with warnings and I know that gcc complains about the 5'th line as **a is actually of type char and not a pointer type. However running the code shows that 'a' and '*a' are actually the same pointer but '**a' is as expected something else (0x71 which I assume is related to 'q' in some way).
I'm trying to make sense of this and it seems that because *a and a are equal **a must also be equal to a because **a=*(*a)=*(a)=*a=a. It seems that the only error in this reasoning can be the types of a, *a, **a.
How is 'a' actually stored in memory? If 'a' is a pointer to another memory location (in my case 0x7fff9841f250) then surely *a should be the value at that memory address which in my case is also 0x7fff9841f250. So then **a would be the value at 0x7fff9841f250 which is the same value as 'a'... It seems that I cannot view the 2d array 'char a[1][1]' as pointers in a way that makes sense. But then how can I think of this type? What is the type and what does a, *a, **a, a[0], a[0][0] actually mean?
Can anyone help me make sense of this?
(*ptr) (*ptr+1) (*ptr+2)
| | |
__________ ______v____________v____________v____________
ptr------>| *ptr |--->| *(*ptr) | *(*ptr+1) |*(*ptr+2) | |
|__________| |____________|_____________|__________|_______|
(ptr+1)--->| *(ptr+1) | ____________ _____________ __________________
|__________|--->|*(*(ptr+1)) |*(*(ptr+1)+1)| | |
| | |____________|_____________|__________|_______|
|__________| ^ ^
| |
*(ptr+1) *(ptr+1)+1
2D array with double pointers that means that you have a main array and the elements of the main array are pointers (or addresses) to a sub arrays. As indicated in above figure
so if you have defined a double pointer as a pointer of this 2D array let's say int **ptr
so ptr is ponting to the main array which will contains pointers to sub arrays. ptr is ponting to the main array that's means ptr is pointing to the first element of the main array so ptr + 1 is pointing to the second element of the main array.
*ptr this means the content of the first element which the ptr is pointing on. And it is a pointer to a subarray. so *ptr is a pointer to the first subarray (the subarray is an array of int). so *ptr is pointing to the first element in the first subarray. so *ptr + 1 is a pointer to the second element in the first subarray
*(ptr+i) is equals to ptr[i] and
*(ptr+1) is ptr[1].
You can think, a 2-D array as array of array.
ptrpoints to complete 2-D array, soptr+1points to next 2-D array.
In figure below ptr is 2-D and number of columns are 3
Original figure made by Mr. Kerrek SB, here , you should also check!
+===============================+==============================+====
|+---------+----------+--------+|+----------+---------+--------+|
||ptr[0,0] | ptr[0,1] | ptr[0,2]|||ptr[1,0] |ptr[1,1] | ptr[1,2]|| ...
|+---------+----------+--------+++----------+---------+--------++ ...
| ptr[0] | ptr[1] |
+===============================+===============================+====
ptr
*(*ptr+1) = *( ptr[0] + 1 ) = ptr[0][1]
Understand following:
ptr points to complete 2-D.
*ptr = *(ptr + 0) = ptr[0] that is first row.
*ptr + 1 = ptr[1] means second row
*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]
Array 0 Elements:
1 2 3 4
And GDB Output:
(gdb) p *(*ptr+1)
$1 = 2
that is correct 2 this can be read using ptr[0][1].
How do you create a single pointer array? You do this:
int* myArray = new int[n];
What does this mean? It has two parts. First part is reserving a pointer int* we call it myArray, and the second part is that you reserve n elements, each with size int in memory (this is an array, right?), and you take the address of that array and you save it in the variable myArray.
Now you want a 2D array, which is an array of an array. So Every element of this new array of array is one of these, that we talked about up there. How do we reserve this? We do:
new int*[n];
Because we are reserving n slots, each with type int*, that we talked about before.
Now what is the type of the return value? It's an array of an array, or a "pointer to an array, and the latter is also a pointer to an array", so you write it as
(int*)*
Or
int**
so it becomes
int** array = new int*[n];
int** array is a pointer to a pointer to an int. So by doing this:
int** array = new int*[n];
you are creating a section of memory that holds n int* pointers and pointing array at that memory. For each of these pointers that you have created, it is possible to create a set of ints like so:
for (auto i = 0; i < n; ++i)
array[i] = new int[n];
The resulting memory will look like this:
array -> [ int* | int * | .... n
[int | int | ...][int | int | ...][ ... n
This is however, much much easier if you use some of the std things in c++, ie a std::vector :
std::vector<std::vector<int>> arr(std::vector<int>(0, n), n);
and you are done ...