0

When using 3, 4 or 5 as dimensions the function gives me the expected output but when I use 6 and above my output looks like this

1  32766 958277840 32766 5  6  6 
0  0  0  958277824 32766 -1131820608  32665 
0  0  17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 
36 37 38 39 40 41 42 
43 44 45 46 47 48

Here're my init() and draw() functions:

int *init(void)
{
    d = *ip;
    int a = d*d;
    int board[a-1];
    int b;
    for (int i=0; i<(a-1); i++)
    { 
        b = i + 1;
        board[i] = b;
    }   
    printf ("\n");
    int *pr;
    pr = board;
    return pr;
}

/**
 * Prints the board in its current state.
 */
void draw(int *pr)
{
    int a = d*d;
    for (int i = 0; i<(a-1); i++)
    {
        int c = pr[i];
        if(c>9)
        {
            printf("%i ",  c);
        }
        else(printf("%i  ", c));
        if((i+1)% (int) d==0)
            printf("\n");
    }
    printf(" \n");
}

Using the debugger I noticed that c randomly changed its value but I couldn't figure out why.

1

Without seeing the entirety of the code, it's very difficult to know what's happening. Having said that, I am concerned about some things here. First, in the original code distribution, board was a global 2-d array. Your code creates what appears to be a shadow variable array of board as a 1-dimensional array in init(). Your code then creates a pointer to it and returns that pointer, which appears to be later passed to draw(). Since the shadow of board only existed inside of load, I'm thinking that it is being released and partially overwritten, and then later used when that pointer is accessed.

I'm also concerned that if you get this working with a 1-d board array, it will still fail check50 because check50 depends on a 2-d array.

Don't know that this answer is going to help much, but if it answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .