简体   繁体   中英

Printing 2D array in a for loop crashes program

I am new to the programming language C. I am trying to make a simple game where you can walk around in a 2D space and place or remove blocks. I am using the terminal for the graphics. The problem is, I expect the program to print an X character followed by 24 blank lines, however, the program just straight up crashes. I don't understand how or why my code could have crashed.

Here is the code:

#include <stdio.h>

int main() {
    char map[79][24]; /* create the map */
    int xPosition = 0, yPosition = 0; /* set the x and y position for the player */
    int x, y; /* create variables for the for loop */
    for (y = 0; y <= 24; y++) { 
        for (x = 0; x <= 79; x++) {
            map[x][y] = ' ';
        }
    } /* initialize the map */
    map[xPosition][yPosition] = 'X'; /* put the player in their position */
    for (y = 0; y <= 24; y++) {
        for (x = 0; x <= 79; x++) {
            printf("%c", map[x][y]);
        }
        printf("\n");
    } /* display the map on screen */
    return 0;
}

Can you explain the problem and show the correct way to do it. Thanks in advance - justAnotherCoder

EDIT: The problem is fixed now. Thank you for your help.

The program has undefined behavior at least because even in the first loop there is used an initialized variable x

int x, y; /* create variables for the for loop */
for (y = 0; x <= 24; x++) { 
            ^^^^^^^
    for (x = 0; y <= 79; y++) {
        map[x][y] = ' ';
    }
} /* initialize the map */

And moreover the conditions x <= 24 and y <= 79 in the loops are also invalid.

The loops do not have sense.

The array map is declared like

char map[79][24];

that is it has 79 rows and 24 columns.

So the loops should look like

for (x = 0; x < 79; x++) { 
    for (y = 0; y < 24; y++) {
        map[x][y] = ' ';
    }
} /* initialize the map */

The same indices and conditions should be use in the second pair of loops.

Instead of the loops that initialize the array you could use the standard string function memset like

#include <string.h>

//,,,

memset( map, ' ', 79 * 24 );

Here is a demonstrative program (for simplicity I reduced the number of rows from 79 to 24)

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

int main(void) 
{
    enum { M = 24, N = 24 };

    char map[M][N];

    memset( map, ' ', M * N );

    map[0][0] = 'X';


    for ( size_t i = 0; i < N + 2; i++ )
    {
        putchar( '-' );
    }
    putchar( '\n' );

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "|%.*s|\n", N, map[i] );
    }

    for ( size_t i = 0; i < N + 2; i++ )
    {
        putchar( '-' );
    }
    putchar( '\n' );

    return 0;
}

The program output is

--------------------------
|X                       |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
--------------------------

Edit: Take into account that after you changed your code nevertheless if to ignore the invalid conditions then you are outputting a figure that contains 80 columns and 25 rows instead of outputting a figure with 25 columns and 80 rows

for (y = 0; y <= 24; y++) {
    for (x = 0; x <= 79; x++) {
        printf("%c", map[x][y]);
    }
    printf("\n");
} /* display the map on screen */

because the inner loop outputs sequentially in one line 80 characters.

Check for loops condition you have written in your code. for achieving the expected result use below modified code

#include <stdio.h>

int main() {
    char map[79][24]; /* create the map */
    int xPosition = 0, yPosition = 0; /* set the x and y position for the player */
    int x, y; /* create variables for the for loop */
    for (x = 0; x < 24; x++) {
        for (y = 0; y < 79; y++) {
            map[x][y] = ' ';
        }
    } /* initialize the map */
    map[xPosition][yPosition] = 'X'; /* put the player in their position */
    for (x = 0; x < 24; x++) {
        for (y = 0; y < 79; y++) {
            printf("%c", map[x][y]);
        }
        printf("\n");
    } /* display the map on screen */
    return 0;
}

Edit: keep the variables scope with assigned blocks of memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM