# Thread: Array prints strange numbers?

1. ## Array prints strange numbers?

What I am trying to do is to have the program create 2D array that for each row, no two numbers are the same and only consists numbers between 1-9. The array lists out every possible combinations. Supposely I do not know the number of possible combinations, I had it keep adding new rows until there are no more possiblities left.

The problem with my code is when I print out the completed array, it gives out really, really large numbers. But when I print the array from inside the first loop, it gives correct values. I do not know exactly what happened. Here is the code:

Code:
```#include <stdio.h>
int main(void)
{
int double_digit[1][2];

int a = 1;
int b = 1;

int i = -1;
int c = 0;
int d = 0;
int f = 0;

for ( a = 1; a < 10; a++)
{
for ( b = 1 ; b < 10; b++)
{
if (a != b)
{
i++;
if (i == 0)
{
double_digit[i][0] = a;
double_digit[i][1] = b;
}
else
{
int new_array[i+1][2];
for (c = 0; c < i; c++)
{
new_array[c][0] = double_digit[c][0];
new_array[c][1] = double_digit[c][1];
}
int double_digit[i+1][2];
for (d = 0; d < i; d++)
{
double_digit[d][0] = new_array[d][0];
double_digit[d][1] = new_array[d][1];
}
double_digit[i][0] = a;
double_digit[i][1] = b;

//                    printf("%d ",double_digit[i][0]);
//                    printf("%d ",double_digit[i][1]);
//                    printf("%d ",a);
//                    printf("%d \n",b);
}
}
}
}

for (f = 0; f < i+1; f++)
{
printf("%d %d \n",double_digit[f][0],double_digit[f][1]);
}

return 0;
}```

2. Because arrays double_digit (line 4) and double_digit (line 34) are different.

3. Because arrays double_digit (line 4) and double_digit (line 34) are different.
You are correct, however, the old values are stored in new_array (see lines 28-33) then later stored in the new double_digit array.

4. And yet, it doesn't work.

Sure it's a new fresh array called double_digit, but it's life begins on line 34, but is tragically cut short on line 47 when the block it was declared in goes out of scope.

By the time you try to print out things on line 53, all your shenanigans has been lost.

You can't resize an array by redeclaring it.
So just declare on line 4 how big you want it to be to begin with.
int double_digit[10][10];
seems like a good place to start.

5. Thanks, I understand now. So there is absolutely no way to resize the double_digit array while keeping all the data?

6. Originally Posted by vxs8122
So there is absolutely no way to resize the double_digit array while keeping all the data?
If you want to resize an array, the normal way is to use dynamic memory. This adds complexity. Here is an example:

Code:
```#define ROWS 2
int **make_array2d(int cols) {
int **arr = realloc(NULL, sizeof(int) * ROWS);
for (int i=0; i < ROWS; i++) {
arr[i] = realloc(NULL, sizeof(int) * cols);
}
return arr;
}

int **grow_array2d(int **arr, int cols) {
arr = realloc(arr, sizeof(int) * ROWS);
for (int i=0; i < ROWS; i++) {
arr[i] = realloc(arr[i], sizeof(int) * cols);
}
return arr;
}

int main(void)
{
// make array 2x2
int **double_digit = make_array2d(2);

double_digit[0][0] = 11;
double_digit[0][1] = 12;

// resize array to 2x4
double_digit = grow_array2d(double_digit,4);
double_digit[0][2] = 13;
double_digit[0][3] = 14;
double_digit[1][0] = 21;
double_digit[1][1] = 22;
double_digit[1][2] = 23;
double_digit[1][3] = 24;

// print result
for (int i=0; i < 2; i++) {
for (int j=0; j < 4; j++) {
printf("%4d", double_digit[i][j]);
}
printf("\n");
}

return EXIT_SUCCESS;
}```
Note there is no error checking, which normally should be used. And the number of rows is fixed. To make it unfixed you would need to have a way to track the old number of rows and the new number of rows, and grow_array2d should act accordingly.