C - strange behaviour of array

Hello everyone!

I have a problem, which seems like a compiler error to me.

I posted my solution of “Power of Thor” below, which works 100% even though I think it shouldnt. The problem is the following line:
char move[1];
This line should declare as far as I know an array of char with only one element. But this array holds 2 chars, which I can read and write without problems.

In an earlier version of the code the line was:
char move[2];
This declared an array with 3 (!) chars, which lead to problems, because the complete string would have unwanted values in char[2]. Only 2 chars are necessary for the solution of this puzzle.

Am I wrong with thinking that char move[i] declares an array with i chars?

Interestingly “sizeof(move)” gives the value, that I think of as being correct.
In case you want to check the code yourself, here is the complete code:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
int lightX; // the X position of the light of power
int lightY; // the Y position of the light of power
int initialTX; // Thor’s starting X position
int initialTY; // Thor’s starting Y position
scanf("%d%d%d%d", &lightX, &lightY, &initialTX, &initialTY);

// game loop
while (1) {
    int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
    char move[1]; // Strange, I thought this would initialize only one char :O
    
    scanf("%d", &remainingTurns);
    
    // resets move
    for (int i = 0; i<2; i++) {
        move[i] = NULL;
    }
    
    // Write an action using printf(). DON'T FORGET THE TRAILING \n
    // To debug: fprintf(stderr, "Debug messages...\n");

    // Y-Axis
    if (initialTY < lightY) {
        move[0] = 'S';            
        ++initialTY;
    } else if (initialTY > lightY) {
        move[0] = 'N';            
        --initialTY;
    }
    
    // X-Axis
    if (initialTX < lightX) {
        move[1] = 'E';            
        ++initialTX;
    } else if (initialTX > lightX) {
        move[1] = 'W';            
        --initialTX;
    }

    // move[0] can't be empty
    if (move[0] == NULL) {
        move[0] = move[1];
        move[1] = NULL;
    }
    
    fprintf(stderr, "move[0]: %c\n",move[0]);
    fprintf(stderr, "move[1]: %c\n",move[1]);
    fprintf(stderr, "move before printing: %s\n",move);
    fprintf(stderr, "size of move: %d\n",sizeof(move));
    
    // A single line providing the move to be made: N NE E SE S SW W or NW
    printf("%s\n",move);
}

return 0;

}

No, it declares an array of 2 chars. But you need 3 to have a delimiter, as strings are ‘\0’ terminated.
Right now you write in some memory behind the array. C allows you to do so, but it’s definitely possible that your program crashes that way. You don’t know in what order the variables are in the memory, you might write some nonsense to the position of Thor without knowing.

Note that an array isn’t initialized when you declare it, so
char move[3] = { 0, 0, 0 };
is probably what you want.

I posted my solution of “Power of Thor” below, which works 100% even though I think it shouldnt.

That’s definately a first! I like it.

Regarding your problem, your « move » variable is automatically allocated on the stack (as opposed to dynamically allocated on the heap). In memory, the stack grows downward from the top and the heap does the opposite. When you call a C function, its parameters are stacked first, then the auto variables come after (and un-stacked when exiting their range). Like your « move » is the last variable, you are just overwriting the unintialized memory without any exception since C doesn’t check for buffer overflow. Just switch your « move » and « remainingTurns » to start to see funny things happen in your code.