This is deadly confusing is such array
int tab[10][10];
a lite/solid block of 100 ints (equivalent of tab[a+b*10] where a is index of right [] and b index of left [] I mean [a]) or this is really (as someone say
but it is really hard to belive for me, an array of 10 pointers to 10-int chunks)?
(so really its size is 100 ints + 10 pointers and this pointers can lay far from int contents?)
could comeone finaly explain it to me (but please tell me a sure/solid answer youre sure of, TNX)
Moving to For Beginners, and including a bit of a longer description.
Multidimensional arrays in C and in C++ can be a bit tricky if you are coming from another language.
They work differently from how they work in Java, C#, and several others.
The line you have above, int tab[10][10]; creates a block of 100 int values. The name of the block is "tab".
It does not create an array of pointers to other arrays, which is how several other languages work.
Effectively the object "tab" points to the first value, very similar to the way a pointer works, but it is not really a pointer. It is the name of the array. The language allows the name to work almost identically to a pointer, and it can automatically get interpreted as a pointer, but it is really the name of the array.
You can access the first item in the array as tab[0][0], or as tab[0], or as *tab. The compiler treats all of these the same.
Accessing other values gets more interesting.
Logically, when you are using the array, it looks like a square shape, with 10 rows of 10 columns.
0, 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, ... etc.
In memory, it looks like a flat line of integers, 100 spaces long.
0, 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, ... etc.
The compiler keeps track of the "tab" variable for you as a square block as long as you use array notation. But when you start using pointer notation, it automatically switches to the second, linear format.
You can access elements of the array in several ways.
First, by using its name in array form. tab[0][0] is 0, tab[0][1] is 1, tab [0][2], is 2. These all follow the square block notation. Continuing down row index 0 gives us tab[0][8] is 8. tab[0][9] is 9. ... And then we have the trickery that comes at the end of a row.
The compiler knows (because we defined a [10][10] array) that each row is 10 lines long. They are treated as offset values, so 10 values are 0 to 9. We can access the next element as tab[1][0] for 10. That will cross to the next row. But that is not the only way to cross rows.
If you refer to the memory as pointers instead of arrays, it gives you lines like this:
int tab[10][10]; int* ptr = tab; ++ptr; then the pointer ptr points at the second element. Using ++ptr will advance to the third element. Another ++ptr will continue for the next value, ++ptr advancing again, all the way through the 100 values in your array. Just be careful, since you are only guaranteed to have 100 blocks of memory. You can advance one space past the end, but trying to actually use that space is undefined. It might work fine, it might crash your program, it might format your hard drive. Be careful about the end of arrays.
Internally, the language does a bit of fancy work for you. When you use a multidimensional array notation, the compiler can treat several things as equivalent. When you define an array with sizes (we'll call them ROWS and COLUMNS), such the definition "int tab[ROWS][COLUMNS];", the compiler can treat an array notation tab[x][y] as though you used the pointer notation "*(tab+(COLUMNS*x)+y)". You don't need to write out the multiplication and addition yourself, just writing tab[x][y] allows the compiler to do that work for you.
The language doesn't care if the programmer uses array notation or pointer notation, it can easily switch between the two. The compiler is just trying to figure out the right spot in memory, and for C, it doesn't matter what route you use as long as you reach the proper destination.
When you use array notation, the language knows that a multidimensional array has a specific size. When you use tab[x][y] it knows that you are trying to reach row x column y of your block of memory, and internally will find the offset with multiplication and addition, using the"*(tab+(COLUMNS*x)+y)" formula internally to interpret the array.
When using array notation, if you use fewer specifiers it will still do the same work. It will assume an unspecified index is zero. tab[1] is the same as tab[1][0] and refers to value 10, tab[2] is the same as tab[2][0] and refers to value 20, tab[3] is the same as tab[3][0] and refers to value 30, and so on. The compiler knows the size and will still do the math to find the right offset it memory.
This also means that tab[1][0], tab[0][10], and tab[2][-10] all refer to the same location in memory. You could also use *(tab+10) or *(tab+(10*1)+0) or many other ways to reach the same slot of memory. All that matters is that when the computer adds and multiplies and does all the math, the number for the memory slot is correct.
An array in C is a contiguous block of memory, the size of all the array dimensions multiplied together. When you use array notation the compiler does the math for you to find the offset into that block of memory. When you use pointer notation you need to do the math to find the offset.
You can use this for any size of multidimensional array.
If we had three dimensions, and name the dimensions RANK, ROWS, and COLUMNS, then defined a variable like "int foo[RANK][ROWS][COLUMNS];" we will make single block of integers that is RANK * ROWS * COLUMNS long. The formula applies just as well to additional dimensions. If you used foo[a]
[c] it is identical to using "*(foo+(a*(ROWS*COLUMNS))+(b*COLUMNS)+c)". It will do all the math internally to find the correct offset inside the block of memory.
This is very different behavior from languages like Java and C#, where multidimensional arrays work differently, which seems to be the source of your confusion. The closest thing to the Java and C# behavior is what Aardvajk wrote above. In that case you need to specify an array of arrays rather than array of int values, and then create new internal arrays. These will not be located consecutively in memory, but may be scattered everywhere. You also cannot access them with a continuous pointer notation, you will need to dereference the first array, then dereference into the second array.