Passing multidimesional array in C/C++ - c++

Why in C/C++ it is needed for a func param that receives a MD arr to has the sizes of all sub arrays/dimensions?
here(PDF): It says the only difference of MD arrs is that
"the compiler remembers each imaginary dimension" but when I violate these dimensions compiler does nothing, e.g.:
char arr[3][5];
arr[0][5] = 10;
So, what's the point of remembering those sizes?

Indexed access into an array must compute memory offset calculations in row-major order based on the index value and declared inferior dimensions. More on that in a bit.
But first, your question is tightly related to this simple observation:
void foo( char a[] )
{
a[5] = 'a';
}
// caller of foo() from somewhere
char arr[5];
foo(arr);
Why does the compiler let you do that ?? Because this is C, and you're perfectly within your rights to shoot your own foot off with undefined behavior. Keeping this in mind:
void foo( char a[][5] )
{
a[0][5] = 'a';
}
// caller of foo() from somewhere
char arr[4][5];
foo(arr);
It is just as "valid" as the prior code (i.e. you're well within your right to enter into UB at your own risk and peril). In this case it will "work", but only because the linear backdrop of the underlying array is twenty elements wide, and we're only accessing the sixth element, which is technically arr[1][0].
The purpose of those inferior dimensions is to properly calculate access like this:
void foo( char a[][5] )
{
a[2][1] = 'b';
}
The 2 superior index must use the declared inferior dimension (in this case 5) to effectively calculate the the linear offset of the proper element. Laying out the 2D array in a 1D linear block, it is used to do this:
char arr[20]; // 4*5
arr[2*5+1] = 'b';
Note the 5. It, the declared inferior dimension, must be known to properly calculate the row-leap (figure of speech).
I hope that makes it at least a little clearer.
I should note that this compounds. I.e., the following:
char arr[3][4][5];
arr[1][2][3] = 'c';
effectively computes the correct location against the linear backdrop of the underlying array:
char arr[60]; // 3*4*5
arr[ 1*(4*5) + 2*(5) + 3 ] = 'c';
And so on. Take that out to as many dimensions as you desire. All the inferior dimensions must be known to do this properly.

An array is not a special kind of object, it's just a long list of items. Your arr[3][5] is in fact just an arr[15] and the arr[0][5] is then redirected to arr[5] by the compiler.
Because C/C++ don't store the sizes, you need to hardcode them to make the [0][5] map to [5] correctly.
Some compilers might enforce the [0][5] to be wrong (or warn about it), but since it maps to [5], it'll do at least something.

Related

Difference between char** and char*

So I have a 2D array (I think) in my main:
char* map[width][height] = {MATRIX HERE};
And I'm passing it to a function:
bool canAccessEdge (char** map, int size, int currentPosX, int currentPosY);
I'm passing with with via:
canAccessEdge (*map, 10, playerStartPosX, playerStartPosY);
I want to traverse the map array and mark the positions that the player was already on. But I understand I can't change a value at an index in the function.
I understand that char map [][] would be an array and char *map is a pointer. So what is char* map [][]? Is there a work around without changing the prototype?
Sorry if these are dumb questions... I can't grasp pointers/arrays too well.
bool exitPossible (char** map, int size, int currentPosX, int currentPosY) {
...
*maze[currentPosX][currentPosY] = "V"; // Operand of '*' must be a pointer but has type "char"
(&maze)[currentPosX][currentPosY] = "V"; // Changes whole row and not the right position
&(maze)[currentPosX][currentPosY] = "V"; // Expression must be a modifiable lvalue (I assume it's immutable)
...
}
char[N] can degrade to char*. That does not mean that char[N][M] can degrade to char**. Beginners always make this assumption but it is not true.
char*[N][M] is something else again. That is not a 2D array of char (which seems to be what you want). A 2D array of chars is char[N][M]. With a 2D array of chars the correct syntax is
maze[currentPosX][currentPosY] = 'V';
Note the single quotes.
If you want to pass a 2D array of chars to a function then the simplest way is to declare the function with a 2D array of chars
bool exitPossible(char maze[N][M], ...)
but the reality is that maze here is a pointer to a 1D array, it's actually not possible to have an array as a function parameter in C++. So the truthful declaration of the function is
bool exitPossible(char (*maze)[M], ...)
Here we see what maze really is, a pointer to a 1D array (of size M). And this completes the first paragraph above. A 2D array char[N][M] degrades not to char** but to char (*)[M]. If this seems complicated then you are right, a good book is needed to explain it fully.
Note that in all this discussion N and M are constants. In C++ array sizes cannot be variable.

Why can't I initialize a two dimensional array with a pointer to a pointer to an int? [duplicate]

I need a pointer to a static 2-dimensional array. How is this done?
static uint8_t l_matrix[10][20];
void test(){
uint8_t **matrix_ptr = l_matrix; //wrong idea
}
I get all kinds of errors like:
warning: assignment from incompatible pointer type
subscripted value is neither array nor pointer
error: invalid use of flexible array member
Here you wanna make a pointer to the first element of the array
uint8_t (*matrix_ptr)[20] = l_matrix;
With typedef, this looks cleaner
typedef uint8_t array_of_20_uint8_t[20];
array_of_20_uint8_t *matrix_ptr = l_matrix;
Then you can enjoy life again :)
matrix_ptr[0][1] = ...;
Beware of the pointer/array world in C, much confusion is around this.
Edit
Reviewing some of the other answers here, because the comment fields are too short to do there. Multiple alternatives were proposed, but it wasn't shown how they behave. Here is how they do
uint8_t (*matrix_ptr)[][20] = l_matrix;
If you fix the error and add the address-of operator & like in the following snippet
uint8_t (*matrix_ptr)[][20] = &l_matrix;
Then that one creates a pointer to an incomplete array type of elements of type array of 20 uint8_t. Because the pointer is to an array of arrays, you have to access it with
(*matrix_ptr)[0][1] = ...;
And because it's a pointer to an incomplete array, you cannot do as a shortcut
matrix_ptr[0][0][1] = ...;
Because indexing requires the element type's size to be known (indexing implies an addition of an integer to the pointer, so it won't work with incomplete types). Note that this only works in C, because T[] and T[N] are compatible types. C++ does not have a concept of compatible types, and so it will reject that code, because T[] and T[10] are different types.
The following alternative doesn't work at all, because the element type of the array, when you view it as a one-dimensional array, is not uint8_t, but uint8_t[20]
uint8_t *matrix_ptr = l_matrix; // fail
The following is a good alternative
uint8_t (*matrix_ptr)[10][20] = &l_matrix;
You access it with
(*matrix_ptr)[0][1] = ...;
matrix_ptr[0][0][1] = ...; // also possible now
It has the benefit that it preserves the outer dimension's size. So you can apply sizeof on it
sizeof (*matrix_ptr) == sizeof(uint8_t) * 10 * 20
There is one other answer that makes use of the fact that items in an array are contiguously stored
uint8_t *matrix_ptr = l_matrix[0];
Now, that formally only allows you to access the elements of the first element of the two dimensional array. That is, the following condition hold
matrix_ptr[0] = ...; // valid
matrix_ptr[19] = ...; // valid
matrix_ptr[20] = ...; // undefined behavior
matrix_ptr[10*20-1] = ...; // undefined behavior
You will notice it probably works up to 10*20-1, but if you throw on alias analysis and other aggressive optimizations, some compiler could make an assumption that may break that code. Having said that, i've never encountered a compiler that fails on it (but then again, i've not used that technique in real code), and even the C FAQ has that technique contained (with a warning about its UB'ness), and if you cannot change the array type, this is a last option to save you :)
To fully understand this, you must grasp the following concepts:
Arrays are not pointers!
First of all (And it's been preached enough), arrays are not pointers. Instead, in most uses, they 'decay' to the address to their first element, which can be assigned to a pointer:
int a[] = {1, 2, 3};
int *p = a; // p now points to a[0]
I assume it works this way so that the array's contents can be accessed without copying all of them. That's just a behavior of array types and is not meant to imply that they are same thing.
Multidimensional arrays
Multidimensional arrays are just a way to 'partition' memory in a way that the compiler/machine can understand and operate on.
For instance, int a[4][3][5] = an array containing 4*3*5 (60) 'chunks' of integer-sized memory.
The advantage over using int a[4][3][5] vs plain int b[60] is that they're now 'partitioned' (Easier to work with their 'chunks', if needed), and the program can now perform bound checking.
In fact, int a[4][3][5] is stored exactly like int b[60] in memory - The only difference is that the program now manages it as if they're separate entities of certain sizes (Specifically, four groups of three groups of five).
Keep in mind: Both int a[4][3][5] and int b[60] are the same in memory, and the only difference is how they're handled by the application/compiler
{
{1, 2, 3, 4, 5}
{6, 7, 8, 9, 10}
{11, 12, 13, 14, 15}
}
{
{16, 17, 18, 19, 20}
{21, 22, 23, 24, 25}
{26, 27, 28, 29, 30}
}
{
{31, 32, 33, 34, 35}
{36, 37, 38, 39, 40}
{41, 42, 43, 44, 45}
}
{
{46, 47, 48, 49, 50}
{51, 52, 53, 54, 55}
{56, 57, 58, 59, 60}
}
From this, you can clearly see that each "partition" is just an array that the program keeps track of.
Syntax
Now, arrays are syntactically different from pointers. Specifically, this means the compiler/machine will treat them differently. This may seem like a no brainer, but take a look at this:
int a[3][3];
printf("%p %p", a, a[0]);
The above example prints the same memory address twice, like this:
0x7eb5a3b4 0x7eb5a3b4
However, only one can be assigned to a pointer so directly:
int *p1 = a[0]; // RIGHT !
int *p2 = a; // WRONG !
Why can't a be assigned to a pointer but a[0] can?
This, simply, is a consequence of multidimensional arrays, and I'll explain why:
At the level of 'a', we still see that we have another 'dimension' to look forward to. At the level of 'a[0]', however, we're already in the top dimension, so as far as the program is concerned we're just looking at a normal array.
You may be asking:
Why does it matter if the array is multidimensional in regards to making a pointer for it?
It's best to think this way:
A 'decay' from a multidimensional array is not just an address, but an address with partition data (AKA it still understands that its underlying data is made of other arrays), which consists of boundaries set by the array beyond the first dimension.
This 'partition' logic cannot exist within a pointer unless we specify it:
int a[4][5][95][8];
int (*p)[5][95][8];
p = a; // p = *a[0] // p = a+0
Otherwise, the meaning of the array's sorting properties are lost.
Also note the use of parenthesis around *p: int (*p)[5][95][8] - That's to specify that we're making a pointer with these bounds, not an array of pointers with these bounds: int *p[5][95][8]
Conclusion
Let's review:
Arrays decay to addresses if they have no other purpose in the used context
Multidimensional arrays are just arrays of arrays - Hence, the 'decayed' address will carry the burden of "I have sub dimensions"
Dimension data cannot exist in a pointer unless you give it to it.
In brief: multidimensional arrays decay to addresses that carry the ability to understand their contents.
In
int *ptr= l_matrix[0];
you can access like
*p
*(p+1)
*(p+2)
after all 2 dimensional arrays are also stored as 1-d.
G'day,
The declaration
static uint8_t l_matrix[10][20];
has set aside storage for 10 rows of 20 unit8_t locations, i.e. 200 uint8_t sized locations,
with each element being found by calculating 20 x row + column.
So doesn't
uint8_t (*matrix_ptr)[20] = l_matrix;
give you what you need and point to the column zero element of the first row of the array?
Edit: Thinking about this a bit further, isn't an array name, by definition, a pointer? That is, the name of an array is a synonym for the location of the first element, i.e. l_matrix[0][0]?
Edit2: As mentioned by others, the comment space is a bit too small for further discussion. Anyway:
typedef uint8_t array_of_20_uint8_t[20];
array_of_20_uint8_t *matrix_ptr = l_matrix;
does not provide any allocation of storage for the array in question.
As mentioned above, and as defined by the standard, the statement:
static uint8_t l_matrix[10][20];
has set aside 200 sequential locations of type uint8_t.
Referring to l_matrix using statements of the form:
(*l_matrix + (20 * rowno) + colno)
will give you the contents of the colno'th element found in row rowno.
All pointer manipulations automatically take into account the size of the object pointed to. - K&R Section 5.4, p.103
This is also the case if any padding or byte alignment shifting is involved in the storage of the object at hand. The compiler will automatically adjust for these. By definition of the C ANSI standard.
HTH
cheers,
In C99 (supported by clang and gcc) there's an obscure syntax for passing multi-dimensional arrays to functions by reference:
int l_matrix[10][20];
void test(int matrix_ptr[static 10][20]) {
}
int main(void) {
test(l_matrix);
}
Unlike a plain pointer, this hints about array size, theoretically allowing compiler to warn about passing too-small array and spot obvious out of bounds access.
Sadly, it doesn't fix sizeof() and compilers don't seem to use that information yet, so it remains a curiosity.
You can always avoid fiddling around with the compiler by declaring the array as linear and doing the (row,col) to array index calculation by yourself.
static uint8_t l_matrix[200];
void test(int row, int col, uint8_t val)
{
uint8_t* matrix_ptr = l_matrix;
matrix_ptr [col+y*row] = val; // to assign a value
}
this is what the compiler would have done anyway.
The basic syntax of initializing pointer that points to multidimentional array is
type (*pointer)[1st dimension size][2nd dimension size][..] = &array_name
The the basic syntax for calling it is
(*pointer_name)[1st index][2nd index][...]
Here is a example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// The multidimentional array...
char balance[5][100] = {
"Subham",
"Messi"
};
char (*p)[5][100] = &balance; // Pointer initialization...
printf("%s\n",(*p)[0]); // Calling...
printf("%s\n",(*p)[1]); // Calling...
return 0;
}
Output is:
Subham
Messi
It worked...
You can do it like this:
uint8_t (*matrix_ptr)[10][20] = &l_matrix;
You want a pointer to the first element, so;
static uint8_t l_matrix[10][20];
void test(){
uint8_t *matrix_ptr = l_matrix[0]; //wrong idea
}
You could also add an offset if you want to use negative indexes:
uint8_t l_matrix[10][20];
uint8_t (*matrix_ptr)[20] = l_matrix+5;
matrix_ptr[-4][1]=7;
If your compiler gives an error or warning you could use:
uint8_t (*matrix_ptr)[20] = (uint8_t (*)[20]) l_matrix;

How to find the number of user-defined elements in an int array?

I have to find the exact number of elements in an int array leaving the '\0's which are effectuated during declaration. I know that the value '\0' is equivalent to 0 .i.e ('\0' == 0) = true.
#include<iostream.h>
int getsize(int arr[])
{
int i = 0,p=1;// initialized to 1 as, if left un-initialised it suffers from undefined behaviour
while(p!=NULL)
{
i++;p=arrr[i]'
}
return i;
}
The code works fine when it is called in the main as:
void main()
{
int testarr[10]={0,5};
cout<<"\nThe size is : "<<getsize(testarr);
}
But when the testarr[] is modified to int testarr[10]={5,0}
The output becomes
The size is : 1
The problem is that the pre-historic turbo c++ compiler,which I'm unfortunately restricted to reads int 0 and '\0' the same.I have used NULL instead but, the code doesn't seem to work. Is there something I'm missing?
How to find the number of user-defined elements in an int array?
There is no way to distinguish a zero that was specified by the programmer, from a zero that was generated by the compiler.
The problem is that the pre-historic turbo c++ compiler,which I'm unfortunately restricted to reads int 0 and '\0' the same.
That behaviour is not specific to your ancient compiler. It is true for all C++ compilers. The value of 0 is exactly the same as '\0'. However, there is no '\0' involved in your program.
while(p!=NULL)
Don't compare an integer with NULL. That is going to confuse anyone reading your code (which since C++11 may be ill-formed depending on how the standard library has chosen to define NULL). Since it is equivalent to comparing with zero, just use while(p).
What your function does, is it counts the number of non-zero elements in the array and until a zero is reached, except the first value is ignored and always counted as 1. If the array does not end in a zero (in other position than first), then the array is accessed over bounds, and there will be UB.
Essentially, it works just like strlen, but for integers, and the first element is treated differently.
So the expected output would be:
{0,0,1,2} -> 1
{0,1,0,0} -> 2
{1,0,0,0} -> 1
{0,0,0,0} -> 1
{0,1,2,0} -> 2
{0,1,2,3} -> Any or no output is expected, because UB
can't I check for null pointers
No. Your array does not contain pointers. It contains integers.
testrarr[10]={5,0} as, by default ,during initialization it fills all the values in the array as \0 a.k.a null character not nullpionter –
No. The array contains integers, so it is not filled with null character. Zero initialization fills the array with zero integers (which has the same value of null character, so the distinction is subtle).
C misuses the term "array" slightly. An array is an organisation of data, a buffer is a place to put data. So when we declare a buffer in C
int testarr[10];
we are declaring a buffer which can hold ten integers, not an array of ten integers. In fact the values may be "illegal integer" trap representations.
When you do
int testarr[10] = {0,5};
you've got a buffer of ten integers, and array of only two. But, partly because the array / buffer confusion is ingrained, C won't tell you. It's normally necessary to keep a separate value, N.
int testarr[10] = {0, 5};
int N = 2;
Now when we pass the array to a subroutine, we pass the address, plus N.
int sum(int *x, int N)
{
int answer = 0;
int i = 0;
for(i=0;i<N;i++)
answer += x[i];
return answer;
}
int testarr[10] = {0, 5}:
int N = 2;
total = sum(testarr, N);
Note that we can also call sum on array slices, or on dynamically allocated arrays. The function is completely unaware of any unused integer slots after the data, or if data is static, on the stack, or on the heap.

How does array declaration work in C++? [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I'm trying to understand the different ways of declaring an array (of one or two dimensions) in C++ and what exactly they return (pointers, pointers to pointers, etc.)
Here are some examples:
int A[2][2] = {0,1,2,3};
int A[2][2] = {{0,1},{2,3}};
int **A = new int*[2];
int *A = new int[2][2];
In each case, what exactly is A? Is it a pointer, double pointer? What happens when I do A+1? Are these all valid ways of declaring matrices?
Also, why does the first option not need the second set of curly braces to define "columns"?
Looks like you got a plethora of answers while I was writing mine, but I might as well post my answer anyway so I don't feel like it was all for nothing...
(all sizeof results taken from VC2012 - 32 bit build, pointer sizes would, of course, double with a 64 bit build)
size_t f0(int* I);
size_t f1(int I[]);
size_t f2(int I[2]);
int main(int argc, char** argv)
{
// A0, A1, and A2 are local (on the stack) two-by-two integer arrays
// (they are technically not pointers)
// nested braces not needed because the array dimensions are explicit [2][2]
int A0[2][2] = {0,1,2,3};
// nested braces needed because the array dimensions are not explicit,
//so the braces let the compiler deduce that the missing dimension is 2
int A1[][2] = {{0,1},{2,3}};
// this still works, of course. Very explicit.
int A2[2][2] = {{0,1},{2,3}};
// A3 is a pointer to an integer pointer. New constructs an array of two
// integer pointers (on the heap) and returns a pointer to the first one.
int **A3 = new int*[2];
// if you wanted to access A3 with a double subscript, you would have to
// make the 2 int pointers in the array point to something valid as well
A3[0] = new int[2];
A3[1] = new int[2];
A3[0][0] = 7;
// this one doesn't compile because new doesn't return "pointer to int"
// when it is called like this
int *A4_1 = new int[2][2];
// this edit of the above works but can be confusing
int (*A4_2)[2] = new int[2][2];
// it allocates a two-by-two array of integers and returns a pointer to
// where the first integer is, however the type of the pointer that it
// returns is "pointer to integer array"
// now it works like the 2by2 arrays from earlier,
// but A4_2 is a pointer to the **heap**
A4_2[0][0] = 6;
A4_2[0][1] = 7;
A4_2[1][0] = 8;
A4_2[1][1] = 9;
// looking at the sizes can shed some light on subtle differences here
// between pointers and arrays
A0[0][0] = sizeof(A0); // 16 // typeof(A0) is int[2][2] (2by2 int array, 4 ints total, 16 bytes)
A0[0][1] = sizeof(A0[0]); // 8 // typeof(A0[0]) is int[2] (array of 2 ints)
A1[0][0] = sizeof(A1); // 16 // typeof(A1) is int[2][2]
A1[0][1] = sizeof(A1[0]); // 8 // typeof(A1[0]) is int[2]
A2[0][0] = sizeof(A2); // 16 // typeof(A2) is int[2][2]
A2[0][1] = sizeof(A2[0]); // 8 // typeof(A1[0]) is int[2]
A3[0][0] = sizeof(A3); // 4 // typeof(A3) is int**
A3[0][1] = sizeof(A3[0]); // 4 // typeof(A3[0]) is int*
A4_2[0][0] = sizeof(A4_2); // 4 // typeof(A4_2) is int(*)[2] (pointer to array of 2 ints)
A4_2[0][1] = sizeof(A4_2[0]); // 8 // typeof(A4_2[0]) is int[2] (the first array of 2 ints)
A4_2[1][0] = sizeof(A4_2[1]); // 8 // typeof(A4_2[1]) is int[2] (the second array of 2 ints)
A4_2[1][1] = sizeof(*A4_2); // 8 // typeof(*A4_2) is int[2] (different way to reference the first array of 2 ints)
// confusion between pointers and arrays often arises from the common practice of
// allowing arrays to transparently decay (implicitly convert) to pointers
A0[1][0] = f0(A0[0]); // f0 returns 4.
// Not surprising because declaration of f0 demands int*
A0[1][1] = f1(A0[0]); // f1 returns 4.
// Still not too surprising because declaration of f1 doesn't
// explicitly specify array size
A2[1][0] = f2(A2[0]); // f2 returns 4.
// Much more surprising because declaration of f2 explicitly says
// it takes "int I[2]"
int B0[25];
B0[0] = sizeof(B0); // 100 == (sizeof(int)*25)
B0[1] = f2(B0); // also compiles and returns 4.
// Don't do this! just be aware that this kind of thing can
// happen when arrays decay.
return 0;
}
// these are always returning 4 above because, when compiled,
// all of these functions actually take int* as an argument
size_t f0(int* I)
{
return sizeof(I);
}
size_t f1(int I[])
{
return sizeof(I);
}
size_t f2(int I[2])
{
return sizeof(I);
}
// indeed, if I try to overload f0 like this, it will not compile.
// it will complain that, "function 'size_t f0(int *)' already has a body"
size_t f0(int I[2])
{
return sizeof(I);
}
yes, this sample has tons of signed/unsigned int mismatch, but that part isn't relevant to the question. Also, don't forget to delete everything created with new and delete[] everything created with new[]
EDIT:
"What happens when I do A+1?" -- I missed this earlier.
Operations like this would be called "pointer arithmetic" (even though I called out toward the top of my answer that some of these are not pointers, but they can turn into pointers).
If I have a pointer P to an array of someType, then subscript access P[n] is exactly the same as using this syntax *(P + n). The compiler will take into account the size of the type being pointed to in both cases. So, the resulting opcode will actually do something like this for you *(P + n*sizeof(someType)) or equivalently *(P + n*sizeof(*P)) because the physical cpu doesn't know or care about all our made up "types". In the end, all pointer offsets have to be a byte count. For consistency, using array names like pointers works the same here.
Turning back to the samples above: A0, A1, A2, and A4_2 all behave the same with pointer arithmetic.
A0[0] is the same as *(A0+0), which references the first int[2] of A0
similarly:
A0[1] is the same as *(A0+1) which offsets the "pointer" by sizeof(A0[0]) (i.e. 8, see above) and it ends up referencing the second int[2] of A0
A3 acts slightly differently. This is because A3 is the only one that doesn't store all 4 ints of the 2 by 2 array contiguously. In my example, A3 points to an array of 2 int pointers, each of these point to completely separate arrays of two ints. Using A3[1] or *(A3+1) would still end up directing you to the second of the two int arrays, but it would do it by offsetting only 4bytes from the beginning of A3 (using 32 bit pointers for my purposes) which gives you a pointer that tells you where to find the second two-int array. I hope that makes sense.
For the array declaration, the first specified dimension is the outermost one, an array that contains other arrays.
For the pointer declarations, each * adds another level of indirection.
The syntax was designed, for C, to let declarations mimic the use. Both the C creators and the C++ creator (Bjarne Stroustrup) have described the syntax as a failed experiment. The main problem is that it doesn't follow the usual rules of substitution in mathematics.
In C++11 you can use std::array instead of the square brackets declaration.
Also you can define a similar ptr type builder e.g.
template< class T >
using ptr = T*;
and then write
ptr<int> p;
ptr<ptr<int>> q;
int A[2][2] = {0,1,2,3};
int A[2][2] = {{0,1},{2,3}};
These declare A as array of size 2 of array of size 2 of int. The declarations are absolutely identical.
int **A = new int*[2];
This declares a pointer to pointer to int initialized with an array of two pointers. You should allocate memory for these two pointers as well if you want to use it as two-dimensional array.
int *A = new int[2][2];
And this doesn't compile because the type of right part is pointer to array of size 2 of int which cannot be converted to pointer to int.
In all valid cases A + 1 is the same as &A[1], that means it points to the second element of the array, that is, in case of int A[2][2] to the second array of two ints, and in case of int **A to the second pointer in the array.
The other answers have covered the other declarations but I will explain why you don't need the braces in the first two initializations. The reason why these two initializations are identical:
int A[2][2] = {0,1,2,3};
int A[2][2] = {{0,1},{2,3}};
is because it's covered by aggregate initialization. Braces are allowed to be "elided" (omitted) in this instance.
The C++ standard provides an example in § 8.5.1:
[...]
float y[4][3] = {
{ 1, 3, 5 },
{ 2, 4, 6 },
{ 3, 5, 7 },
};
[...]
In the following example, braces in the initializer-list are elided;
however the initializer-list has the same effect as the
completely-braced initializer-list of the above example,
float y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};
The initializer for y begins with a left brace, but the one for y[0]
does not, therefore three elements from the list are used. Likewise
the next three are taken successively for y[1] and y[2].
Ok I will try it to explain it to you:
This is a initialization. You create a two dimensional array with the values:
A[0][0] -> 0
A[0][1] -> 1
A[1][0] -> 2
A[1][1] -> 3
This is the exactly the same like above, but here you use braces. Do it always like this its better for reading.
int **A means you have a pointer to a pointer of ints. When you do new int*[2] you will reserve memory for 2 Pointer of integer.
This doesn't will be compiled.
int A[2][2] = {0,1,2,3};
int A[2][2] = {{0,1},{2,3}};
These two are equivalent.
Both mean: "I declare a two dimentional array of integers. The array is of size 2 by 2".
Memory however is not two dimensional, it is not laid out in grids, but (conceptionaly) in one long line. In a multi-dimensional array, each row is just allocated in memory right after the previous one.
Because of this, we can go to the memory address pointed to by A and either store two lines of length 2, or one line of length 4, and the end result in memory will be the same.
int **A = new int*[2];
Declares a pointer to a pointer called A.
A stores the address of a pointer to an array of size 2 containing ints. This array is allocated on the heap.
int *A = new int[2][2];
A is a pointer to an int.
That int is the beginning of a 2x2 int array allocated in the heap.
Aparrently this is invalid:
prog.cpp:5:23: error: cannot convert ‘int (*)[2]’ to ‘int*’ in initialization
int *A = new int[2][2];
But due to what we saw with the first two, this will work (and is 100% equivalent):
int *A new int[4];
int A[2][2] = {0,1,2,3};
A is an array of 4 ints. For the coder's convenience, he has decided to declare it as a 2 dimensional array so compiler will allow coder to access it as a two dimensional array. Coder has initialized all elements linearly as they are laid in memory. As usual, since A is an array, A is itself the address of the array so A + 1 (after application of pointer math) offset A by the size of 2 int pointers. Since the address of an array points to the first element of that array, A will point to first element of the second row of the array, value 2.
Edit: Accessing a two dimensional array using a single array operator will operate along the first dimension treating the second as 0. So A[1] is equivalent to A[1][0]. A + 1 results in equivalent pointer addition.
int A[2][2] = {{0,1},{2,3}};
A is an array of 4 ints. For the coder's convenience, he has decided to declare it as a 2 dimensional array so compiler will allow coder to access it as a two dimensional array. Coder has initialized elements by rows. For the same reasons above, A + 1 points to value 2.
int **A = new int*[2];
A is pointer to int pointer that has been initialized to point to an array of 2 pointers to int pointers. Since A is a pointer, A + 1 takes the value of A, which is the address of the pointer array (and thus, first element of the array) and adds 1 (pointer math), where it will now point to the second element of the array. As the array was not initialized, actually doing something with A + 1 (like reading it or writing to it) will be dangerous (who knows what value is there and what that would actually point to, if it's even a valid address).
int *A = new int[2][2];
Edit: as Jarod42 has pointed out, this is invalid. I think this may be closer to what you meant. If not, we can clarify in the comments.
int *A = new int[4];
A is a pointer to int that has been initialized to point to an anonymous array of 4 ints. Since A is a pointer, A + 1 takes the value of A, which is the address of the pointer array (and thus, first element of the array) and adds 1 (pointer math), where it will now point to the second element of the array.
Some takeaways:
In the first two cases, A is the address of an array while in the last two, A is the value of the pointer which happened to be initialized to the address of an array.
In the first two, A cannot be changed once initialized. In the latter two, A can be changed after initialization and point to some other memory.
That said, you need to be careful with how you might use pointers with an array element. Consider the following:
int *a = new int(5);
int *b = new int(6);
int c[2] = {*a, *b};
int *d = a;
c+1 is not the same as d+1. In fact, accessing d+1 is very dangerous. Why? Because c is an array of int that has been initialized by dereferencing a and b. that means that c, is the address of a chunk of memory, where at that memory location is value which has been set to the value pointed to by tovariable a, and at the next memory location that is a value pinned to by variable b. On the other hand d is just the address of a. So you can see, c != d therefore, there is no reason that c + 1 == d + 1.

Why can I declare a 2D array with both dimensions sized variable but not new one?

As the problem stated, this is doable:
#include <iostream>
int main(int argc, char *argv[])
{
unsigned short int i;
std::cin >> i;
unsigned long long int k[i][i];
}
Here I declared an array that is sized i by i, both dimensions are variables.
But not this:
#include <iostream>
int main(int argc, char *argv[])
{
unsigned short int i;
std::cin >> i;
unsigned long long int** k = new int[i][i];
delete[] k;
}
I got an compiler message telling me that
error: only the first dimension of an allocated array may have dynamic
size
I am forced to do this:
#include <iostream>
int main(int argc, char *argv[])
{
unsigned short int i;
std::cin >> i;
unsigned long long int** k = new unsigned long long int*[i];
for ( unsigned short int idx = 0 ; idx < i ; ++ i )
k[idx] = new unsigned long long int[i];
for ( unsigned short int idx = 0 ; idx < i ; ++ i )
delete[] k[idx];
delete[] k;
}
To my understanding, new and delete are used to allocate something on heap, not on stack, which won't be deleted when it goes out of scope, and is useful for passing datas across functions and objects, etc.
What I don't understand is what happens when I declare that k in the first example, I am told that declared array should (and could) only have constant dimensions, and when in need for a array of unknown size, one should always consider new & delete or vectors.
Is there any pros and cons to those two solutions I'm not getting, or is it just what it is?
I'm using Apple's LLVM compiler by the way.
Neither form is C++ standard compliant, because the standard does not support variable-length arrays (VLAs) (interestingly, C99 does - but C is not C++). However, several compilers have an extension to support this, including your compiler:
From Clang's Manual:
Clang supports such variable length arrays in very limited circumstances for compatibility with GNU C and C99 programs:
The element type of a variable length array must be a POD ("plain old data") type, which means that it cannot have any user-declared constructors or destructors, any base classes, or any members of non-POD type. All C types are POD types.
Variable length arrays cannot be used as the type of a non-type template parameter.
But given that the extension is in place, why doesn't your second snippet work? That's because VLA only applies to automatic variables - that is, arguments or local variables. k is automatic but it's just a pointer - the array itself is defined by new int[i][i], which allocates on the heap and is decidedly not an automatic variable.
You can read more about this on the relevant GCC manual section.
I'm sure you can find implementation for 2D array functionality easily, but you can make your own class too. The simplest way is to use std::vector to hold the data and have an index-mapping function that takes your two coordinates and return a single index into the vector.
The client code will look a little different, instead of arr[x][y] you have arr.at(x,y) but otherwise it does the same. You do not have to fiddle with memory management as that is done by std::vector, just use v.resize(N*N) in constructor or dimension-setting function.
Essentially what compilers generally do with two-dimensional arrays (fixed or variable) is this:
int arr[x][y] ---> int arr[x*y];
arr[2][4]= something ---> arr[2+4*x]= something;
Basically they are just a nicer way of notation of a one-dimensional array (on the stack). Most compilers require fixed sizes, so the compiler has an easier way of telling what the dimensions are (and thus what to multiply with). It appears you have just a compiler, which can keep track of the dimensions (and multipliers) even if you use variables.
Of course you can mimick that with new[] yourself too, but it's not supported by the compiler per se.
Probably for the same reason, i.e. because it would be even harder keeping track of the dimensions, especially when moving the pointers around.
E.g. with a new-pointer you could later write:
newarr= someotherarray;
and someotherarray could be something with even different dimensions. If the compiler did a 2-dim -> one dim translation, he'd have to track all possible size transitions.
With the stack allocated arr above, this isn't necessary, because at least once the compiler made it, it stays that size.