小编chi*_*hib的帖子

填充2D char数组会导致C崩溃

我试图用文本文件中的字符填充二维数组(mapLayout).

当我使用printf输出字符时,一切看起来都很好,但是将数字添加到数组的实际行似乎导致了崩溃.

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

void createMap();

//height of file being read
int mapHeight, mapWidth = 20;
char mapLayout[20][20];

int main()
{
    createMap();
    return 0;
}

//read in string from file and populate mapLayout with chars
void createMap(){
    FILE *file = fopen("map.txt", "r");
    int col, row = 0;
    int c;

    if (file == NULL)
        return NULL; //could not open file

    while ((c = fgetc(file)) != EOF)
    {
        printf("%c", c);
        printf("\nx:%d, y:%d\n", col, row);

        if(c == '\n'){
            row++;
            col = …
Run Code Online (Sandbox Code Playgroud)

c arrays char multidimensional-array

0
推荐指数
1
解决办法
346
查看次数

标签 统计

arrays ×1

c ×1

char ×1

multidimensional-array ×1