Adam Le Roux
Adam Le Roux

Reputation: 43

2D array outputing different values if you print it by line of the entire array in C

Hey StackOverflow community!

I've got a weird thing happening in my C code.

Here the part of the code concerned :

( "tab4" is a 16 length, 1 dimension array, filled with ints, "tab" is a 4x4 2D array filled with 0s)

I'm trying to put the values of tab4 in "tab" (each line of "tab" gets 4 digits of tab4)

I've put some prints to test results

for (int u = 0; u < 4; u++)
        {
            tab[0][u] = tab4[u];
            printf(" %d ", tab[0][u]);
        }
        printf("\n");
        for (int yu = 4; yu < 8; yu++)
        {
            tab[1][yu] = tab4[yu];
            printf(" %d ", tab[1][yu]);

        }
        printf("\n");
        for (int cont = 8; cont < 12; cont++)
        {
            tab[2][cont] = tab4[cont];
            printf(" %d ", tab[2][cont]);
        }
        printf("\n");
        for (int ye = 12; ye < 16; ye++)
        {
            tab[3][ye] = tab4[ye];
            printf(" %d ", tab[3][ye]);
        }
        printf("\n\n");

        for (int i = 0; i < 4; i++)
        {
            printf("\n");
            for (int y = 0; y < 4; y++)
            {
                printf(" %d ", tab[i][y]);
            }
        }
        printf("\n\n");

The weird thing happening here is that when I print "tab" line by line, it works, but when I print it as a whole, it doesn't.

Here is my output :

This is the array, printed line by line
 16  8  4  0
 8  64  0  0
 0  0  0  0
 0  0  0  0

here is the array printed as a whole

 16  8  4  0
 0  0  0  0
 8  64  0  0
 0  0  0  0

This may be linked to some warning I have :

Severity Code Description Project File Line Suppression State Warning C6386 Buffer overrun while writing to 'tab[1]': the writable size is '16' bytes, but '20' bytes might be written.

Severity Code Description Project File Line Suppression State Warning C6385 Reading invalid data from 'tab[1]': the readable size is '16' bytes, but '20' bytes may be read.

(same warning for all "for" loops)

I know it means that I'm trying to write somewhere I shouldn't, but I don't know how I could possibly be doing that

Thanks for the answers!

Upvotes: 1

Views: 69

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

Loops such that

    for (int yu = 4; yu < 8; yu++)
    {
        tab[1][yu] = tab4[yu];
        printf(" %d ", tab[1][yu]);

    }

are incorrect. The second "row" (one-dimensional array) of the array tab has the range of valid indices [0, 4).

So you need it rewrite at least like

    for (int yu = 4; yu < 8; yu++)
    {
        tab[1][yu-4] = tab4[yu];
        printf(" %d ", tab[1][yu-4]);

    } 

Or you could fill the array tab using the following loops

for ( int i = 0, k = 0; i < 4; i++)
{
    for ( int j = 0; j < 4; j++ )
    {
        tab[i][j] = tab4[k++];
        printf(" %d ", tab[i][j]);
    }
    printf("\n");
}

Or

for ( int i = 0; i < 4; i++)
{
    for ( int j = 0; j < 4; j++ )
    {
        tab[i][j] = tab4[4 * i + j];
        printf(" %d ", tab[i][j]);
    }
    printf("\n");
}

Upvotes: 3

Related Questions