简体   繁体   English

当我尝试在C中的简单地图上跨屏幕移动播放器时,程序崩溃

[英]Program crashes when I try to move my player across the screen on a simple map in C

    int main()
    {
        int size, x, y, test = 1;
        printf("How big is your map? ");
        scanf("%d", &size);
        char map[size][size], move;
        int row = size/2, col = size/2;
        for(x = 0; x < size; x++)
        { //gives null value to everything in the board except for the player
            for(y = 0; y < size; y++)
            {
                map[x][y] = 'X';
            }
        }
        while(test != 0)
        {
            scanf("%d", &test);
            for(x = 0; x < size; x++)
            {
                for(y = 0; y < size; y++)
                    if (x == row && y == col)
                    {
                        printf("O");
                        map[row][col] = 'O';
                    }
                    else
                        printf("%c", map[x][y]);
                    printf("\n");
            }
//
// Everything runs completely fine until this point,
// after I type either W, A, S, or D, the program crashes
// and gives me an insanely high return value.
// I have no idea what I did wrong, and any insight would be extremely appreciated.
//
            printf("Use W, A, S, D to move around.");
            map[row][col] = 'X'; //set the old player location to null.
            while(x != 0)
            {
                scanf(" %c", move);
                switch(move)
                {
                    case 'w': 
                    case 'W': row -= 1; x = 0; break;
                    case 's':
                    case 'S': row += 1; x = 0; break;
                    case 'a':
                    case 'A': col -= 1; x = 0; break;
                    case 'd':
                    case 'D': col += 1; x = 0; break;
                    default: printf("Invalid input, try again.");
                }
            }
            map[row][col] = 'O';
        }
        return 0;
    }

Try giving the address of move to scanf() : 尝试move地址提供给scanf()

scanf(" %c", &move);

scanf() needs a destination (address) specifying where to write the character scanned/read. scanf()需要一个目的地 (地址),用于指定在何处写入已扫描/读取的字符。 You are giving it whatever garbage value happens to be in move . 您给它的是垃圾值是否在move It is using this value as an address and trying to write your character there. 它使用此值作为地址并尝试在其中写入您的字符。 Since it is writing to an invalid memory location, it crashes. 由于它正在写入无效的内存位置,因此会崩溃。

Like forgetting ; 喜欢忘记; , forgetting & in scanf is one of the most common mistakes in C programming. ,忘记&使用scanf是C编程中最常见的错误之一。

And you forgot & for move in scanf . 你忘了&用于movescanf

Look into your code, I believe that you have already known the reason why need to pass address to scanf 查看您的代码,我相信您已经知道了为什么需要将地址传递给scanf的原因

But in case you haven't, you could refer this with high quality accepted answer Why scanf must take the address of operator 但是,如果没有,您可以参考高质量的可接受答案, 为什么scanf必须使用运算符的地址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用 C 中的 sqlite 读取 NULL 值时,我的程序崩溃了 - My program crashes when I try to read a NULL value using sqlite in C 运行时简单的C程序崩溃 - simple C program crashes when run 数据结构:当我尝试将数据弹出堆栈时,我的程序崩溃了 - Data Structures: My program crashes when I try to pop my data out of the stack 每当我尝试对旧内存进行 delocate 时,我的程序都会崩溃 - My program crashes whenever i try to delocate the old memory 谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 Insert 或 Delete 函数时我的程序会崩溃? - Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program? 当我尝试使用 .txt 文件时,.exe 程序崩溃 - .exe program crashes when I try to work with a .txt file 从C程序调用C ++ DLL时,当我尝试访问DLL中的调用参数指针时会崩溃 - When calling a C++ DLL from a C program it crashes when I try to access call parameter pointer in the DLL Windows中的简单C程序崩溃 - Simple c program crashes in windows 为什么当我在学期变量中输入正确的值时,我的程序会崩溃? C - why when i put a proper value in semester variable my program crashes? C 当我尝试将结构中的char *类型的元素设置为特定字符串时,C程序崩溃? - C program crashes when I tried to set an element of type char* in my struct to a specific string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM