Home > Mobile >  Weird array behaviour in C
Weird array behaviour in C

Time:07-20

so I'm trying to write a simple function that fills a given area of the screen with 'X's. The Area to be filled is user defined. As (later on) I want to be able to manipulate that display, the 'x's are written into an array. Then, I use this array for my printf().

This works perfectly fine most of the time, but there are two problems:

  1. In some cases ( for Example with the values 5,10) one X is missing from the array! (In this case drawPic[8][0] ) It doesn't make any sense to me...
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
 X X X X
X X X X X
  1. Second, I sometimes get that nice segmentation fault: 11 error. I don't understand why this is the case, it seems quite random. (for example, values 5,12 will give error, values 10,12 will not)
/*
Testcase to fill a picture frame
*/

#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")

int main()
{
        int sizeX, sizeY;
        clrscr();
        printf("\n\n\n\n\tEnter picture size (x,y):\t");
        scanf("%d,%d", &sizeX, &sizeY);
        char drawPic[sizeX-1][sizeY-1];
        clrscr();
        for(int i=0; i<sizeY; i  )
        {
                for(int j=0; j<sizeX; j  )
                {
                        drawPic[i][j] = 'X';
                        if(j<sizeX-1)
                                printf("%c ", drawPic[i][j]);
                        else
                                printf("%c\n", drawPic[i][j]);
                }
        }
        // printf("\n\n 9th line 1st char: %c\n\n", drawPic[8][0]);
}

/edit: OKAY, first of all: thanks a lot. Array size was a typical noob mistake, i guess. Correct array size solved issue no.1, and the other problem was that i had i,j of drawPic the wrong way round.

Working code:

/*
Testcase to fill a picture frame
*/

#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")

int main()
{
        int sizeX, sizeY;
        clrscr();
        printf("\n\n\n\n\tEnter picture size (x,y):\t");
        scanf("%d,%d", &sizeX, &sizeY);
        char drawPic[sizeX][sizeY];
        clrscr();
        for(int i=0; i<sizeY; i  )
        {       
                for(int j=0; j<sizeX; j  )
                {       
                        drawPic[j][i] = 'X';
                        if(j<sizeX-1) 
                                printf("%c ", drawPic[j][i]);
                        else    
                                printf("%c\n", drawPic[j][i]);
                }
        }
}

You're a great community to help out beginners like me with their stupid mistakes which must seem so very obvious to you. Thanks a lot!

CodePudding user response:

Your array is too small:

char drawPic[sizeX-1][sizeY-1];

This creates a 2D array with dimensions sizeX-1 and sizeY-1 which is one less than you requested. Because array indices in C start from 0, the valid indices for these dimensions are 0 to sizeX-2 and 0 to sizeY-2 respectively.

Your loops then use indices up to sizeX-1 and sizeY-1 which means you read and write outside the bounds of the array. This triggers undefined behavior which explains the occasional strange results you're seeing.

When declaring an array, the dimensions specify the total number of elements, not the index of the last element. So you instead should declare it as:

char drawPic[sizeX][sizeY];

You've also got your loop conditions mixed up:

    for(int i=0; i<sizeY; i  )
    {
            for(int j=0; j<sizeX; j  )
            {

i should have sizeX as its limit and j should have sizeY as its limit, not the other way around. This can also cause reading outside the array bounds when the two values are not the same. While it's probably not a problem in practice, there's no guarantee it won't be.

  • Related