1

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!

4
  • Please edit your question adding a minimal reproducible example, starting from the exact declaration of tab4 and tab. Commented May 25, 2020 at 12:13
  • 1
    tab[1][4] does not exist. You want tab[1][yu - 4] = tab4[yu]; ... same for other loops Commented May 25, 2020 at 12:13
  • ""tab" is a 4x4 2D" - which means every loop except the first is wrong. They're all accessing index >= 4 in the inferior dimension of tab. Commented May 25, 2020 at 12:13
  • 1
    what you are trying to do with 4 loops (except the inner prints) can be done with one single statement: memcpy(tab, tab4, sizeof tab4); Commented May 25, 2020 at 12:15

1 Answer 1

3

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

oh my god I can't believe I made this mistake! Thanks for the input I'll be more careful in the future :) !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.