You have good answers above.
But I’m going to leave an example in C of this in practice and maybe illustrate these things better
I was trying to create a matrix using while
and I realized that the program displays all elements correctly if I just increment the value from j (column) up to 8. It was not for the program to print random values, since there are only 3 columns?
C is not FORTRAN. In C there are no matrices or this sort of multi-dimensional vectors.
In C there are only vectors. And vector vectors. And vector vectors. And so on.
The data is saved by line. one after the other. All that exists is the start address and the calculation from the indexes to find the element. There is only base and offset, an address and a value. And that is the general formula:
An example in C
Instead of a vector int[3][3]
as in your program I will use a char[12]
to show the mechanics: 12 is multiple of 2, 3, 4 and 6 after all. I will not use more than 2 indexes because it would be longer for no reason. The above formula shows the account for any number of indices: base(ar)
is the initial address of the array, esize
is the number of columns. The formula is everywhere, but this image has been copied from Data Structures Using C, Tenenbaum and others, '89 A textbook used everywhere.
The program
The following code uses this vector
char x[13] = "0123456789AB"; // 12+1
And then access as vectors 2x6, 3x4, 4x3 and 6x2 , changing the ninth element to '0'
, '1'
and '2'
in the vector char[]
and showing the effect on the other "matrices"
the output of the program
x[8] = '0' = 48 (dec)
x_26[1][2] = '0' = 48 (dec)
x_34[2][0] = '0' = 48 (dec)
x_43[2][2] = '0' = 48 (dec)
x_62[4][0] = '0' = 48 (dec)
x[8] = '1' = 49 (dec)
x_26[1][2] = '1' = 49 (dec)
x_34[2][0] = '1' = 49 (dec)
x_43[2][2] = '1' = 49 (dec)
x_62[4][0] = '1' = 49 (dec)
x[8] = '2' = 50 (dec)
x_26[1][2] = '2' = 50 (dec)
x_34[2][0] = '2' = 50 (dec)
x_43[2][2] = '2' = 50 (dec)
x_62[4][0] = '2' = 50 (dec)
The code C
The program has only 20 lines, serves only to show how to address this in C and as what exists is only base and offset.
#include <stdio.h>
int main(void){
char x[13] = "0123456789AB"; // 12+1
char (*x_26)[2][6] = NULL;
char (*x_34)[3][4] = NULL;
char (*x_43)[4][3] = NULL;
char (*x_62)[6][2] = NULL;
x_26 = x_34 = x_43 = x_62 = x;
for ( int i='0'; i<'3'; i+=1)
{
x[8] = i;
printf( "\nx[8] = '%c' = %d (dec)\n", x[8], x[8]);
printf( "x_26[1][2] = '%c' = %d (dec)\n", (*x_26)[1][2], (*x_26)[1][2] );
printf( "x_34[2][0] = '%c' = %d (dec)\n", (*x_34)[2][0], (*x_34)[2][0] );
printf( "x_43[2][2] = '%c' = %d (dec)\n", (*x_43)[2][2], (*x_43)[2][2] );
printf( "x_62[4][0] = '%c' = %d (dec)\n", (*x_62)[4][0], (*x_62)[4][0] );
}
return 0;
};
That’s the important line:
x_26 = x_34 = x_43 = x_62 = x;
Since all are pointers to the same address, changing the dimensions of the vectors changes the formula above, but the sum will always give 8 :) and, as the basis is also always the same, all pointers point to the same thing.
Here are the accounts:
To char x[12]
: x[8]
will be in *(x + 8)
, displacement 8 from x
To char x[2][6]
: x[1][2]
will be in *(x + 1*6 + 2)
, displacement 8 from x
To char x[3][4]
: x[2][0]
will be in *(x + 2*4 + 0)
, displacement 8 from x
To char x[4][3]
: x[2][2]
will be in *(x + 2*3 + 2)
, displacement 8 from x
To char x[6][2]
: x[4][0]
will be in *(x + 4*2 + 0)
, displacement 8 from x
Okay, it’s bad to read, but you can see the formula in effect. And how to declare these things in C.
In fact there is already an error when declaring a 3x3 matrix assigning the values only at the 0xN position, the statement should be as follows so that it has effectively the two dimensions of the matrix:
int m[3][3] = { {10,2,3}, {4,5,6}, {7,8,9} };
– bruno101
@bruno101 It’s no problem to initialize the array that way. See here in the second example of the "nested arrays" section. See also this answer which shows that deep down the dimensions are just an "illusion" :-)
– hkotsubo