1. ## Multidimensional arrays.

Hi guys,
I'm a noob so please bear with me.
I'm trying to write some values in a multidimensional array with a variable length and then to print the values in row/col format. The problem is that it prints out wrong values. In the case of a 2*2 matrix it prints the last 2 values entered. Why is that?
Don't mind the commented out lines.
Thank you, guys.
Code:
Code:
```#include <stdio.h>
//#define MAX 100

int main(void)
{
int rowA, colA, rowB, colB;
rowA = colA = rowB = colB = 0;
int i, j;
//    int N = -1;
int A[rowA][colA];//, B[0][0], C[0][0];

//    while ((N <= 0) || (N > MAX))
//    {
//        printf("Enter the No. of elements for arrays >>> ");
//        scanf("%i", &N);
//        if ((N > MAX) || (N <= 0))
//        {
//            printf("Up to '100' and bigger than '0'.\n");
//            //printf("Enter the No. of elements for arrays >>> ");
//        }
//    }

printf("\nEnter the No. of rows for matrix 'A' >>> ");
scanf("%i", &rowA);
printf("\nEnter the No. of cols for matrix 'A' >>> ");
scanf("%i", &colA);
//    printf("\nEnter the No. of cols for matrix 'B' >>> ");
//    scanf("%i", &colB);

//    rowB = colA;

for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("\nEnter the value of A[%i][%i] >>> ", i, j);
scanf("%i", &A[i][j]);
printf("%i", A[i][j]); // checking if values has been entered corectly
}
}

//    for (i = 0; i < rowB; i++)
//    {
//        for (j = 0; j < colB; j++)
//        {
//            printf("\nThe value of B[%i][%i] is >>> ", i, j);
//            scanf("%i", &B[i][j]);
//        }
//    }

printf("\nThis is the matrix 'A':\n");
for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("%i ", A[i][j]);
}
printf("\n");
}

return 0;
}```

2. You are not thinking that array resize themselves, right?

3. Originally Posted by Bayint Naung
You are not thinking that array resize themselves, right?
Sorry, what do you mean?

4. Your sizing of the array, is a real problem.

You have to size your arrays by a value that you have set, beforehand. Changing any of the row or column sizes later, will have no effect on the array that was declared with the old value/s.

If you really need to set the row and col variables for the array, then you need to skip declaring the array at first, and just do something like this:

Code:
`int **myArray;`
Then, after you get the right row and column sizes, you can malloc the memory for the array. It's not a really trivial bit of code.

5. Basically, you are declaring an array A[0][0] since rowA and colA are zero when that line gets executed. Then afterward, you are changing the value of rowA and colA. Note that this WILL NOT change the size of the array you declared earlier. The variables rowA and colA are not associated in any way with the array after that declaration line is done.

I managed to pull it off, but I still didn't fully understood what happens at memory level with this so called " array resizing". So it will be great if anyone wishes to direct me to any kind of reference. Thank you, guys.
Here is the code:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int rows, cols, i, j;
int **matrix_A;

printf("\nEnter the No. of rows for matrix 'A' >>> ");
scanf("%i", &rows);
printf("\nEnter the No. of cols for matrix 'A' >>> ");
scanf("%i", &cols);

matrix_A = malloc(rows * sizeof(int *));
for (i = 0; i < rows; i++)
{
matrix_A[i] = malloc(cols * sizeof(int));
}

for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("\nEnter the value of A[%i][%i] >>> ", i, j);
scanf("%i", &matrix_A[i][j]);
}
}

printf("\nThis is the matrix 'A':\n\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%i ", matrix_A[i][j]);
}
printf("\n");
}

return 0;
}```

7. Here is the second solution after KBriggs's comment:

Code:
```#include <stdio.h>

int main(void)
{
int rowA, colA;
rowA = colA = 0;
int i, j;

printf("\nEnter the No. of rows for matrix 'A' >>> ");
scanf("%i", &rowA);
printf("\nEnter the No. of cols for matrix 'A' >>> ");
scanf("%i", &colA);

int A[rowA][colA];

for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("\nEnter the value of A[%i][%i] >>> ", i, j);
scanf("%i", &A[i][j]);
printf("%i", A[i][j]); // checking if values has been entered corectly
}
}

printf("\nThis is the matrix 'A':\n\n");
for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("%i ", A[i][j]);
}
printf("\n");
}

return 0;
}```

8. That looks a LOT better - well done (although I didn't run the code).

Note that there is NO resizing done here - you made a pointer to a pointer - and then gave it a good address with malloc().

Then you repeated that in a loop, but this time, assigning each row pointer, to a valid address.

Resizing arrays would use realloc(), and it has it's own little pattern to be followed. It is best to malloc() more than you expect to use, and then resize the array, in blocks of enough size to prevent having to resize the array, over and over.

One resize of 60 rows, is better than 20 resizes, of 3 rows each.

That looks a LOT better - well done (although I didn't run the code).
Thank you, Adak. I was going out of my mind with this and, I'm still struggling to understand the process at the lowest level. But now I can sleep tonight.

10. Think of an array like an icecube tray... each cell can hold one value. Once it's made, that's how big it is.

You can't change the size of static array because that would displace existing data. Memory is allocated contiguously as row0......row1.......row2..... etc. If you change the size of row 1, you will overwrite the data in row2 and now your array is filled with garbage.

Once you make an array of any size, that's how big it is.

Even using malloc, the array dimensions are determined by the amount of memory you reserved. and trying to plug in an extra row or remove a column will have exactly the same result as with a static declaration. Realloc overcomes this, up to a point, but it's not perfect either.

11. The first solution is bad because it doesn't free what it allocates.

12. Originally Posted by Elysia
The first solution is bad because it doesn't free what it allocates.
Should I use free(matrix_A) after it prints out the matrix values?

13. One free for every call to malloc, in reverse order.