【问题标题】:C Program: square brackets when printing 2D character arrayC程序:打印二维字符数组时的方括号
【发布时间】:2011-04-10 15:05:02
【问题描述】:

我遇到了一个令人困惑的问题 - 至少对我来说,这可能非常简单而且我遗漏了一些东西。我正在尝试初始化一个可变大小的二维数组,该数组是结构的一部分。我在为数组分配内存时没有问题,但是当我尝试将字符分配给数组或打印它们时,我收到了意外的结果 =/

我的最后一次尝试表明,无论我何时将字符分配给数组,当我打印一个 '[' 时都会打印...这是我第一次真正能够打印 anything。我之前的尝试返回了段错误。这是代码,我错过了什么。谢谢。

typedef struct world_map {
    char ** map_array;
    int X, Y;
} MAP_s;
//
MAP_s * map;

int init_map_array(void) {
    int i; // temp
    map = malloc(sizeof (MAP_s));
    map->X = 20; // Columns
    map->Y = 10; // Rows
    //
    map->map_array = malloc(map->Y * (sizeof (char *)));
    //
    if (map->map_array == 0) {
        printf("ERROR: out of memory!");
        return -1;
    } else {
        for (i = 0; i < map->Y; ++i) {
            map->map_array[i] = malloc(map->X * sizeof (char));
            if (map->map_array[i] == 0) {
                printf("ERROR: out of memory!");
                return -1;
            }
        }
    }
    int curr_pos_x, curr_pos_y;
    int limit_x = map->X;
    int limit_y = map->Y;
    //
    for (curr_pos_y = 0; curr_pos_y < limit_y; ++curr_pos_y) {
        for (curr_pos_x = 0; curr_pos_x < limit_x; ++curr_pos_x) {
            map->map_array[curr_pos_y][curr_pos_x] = "#";
        }
    }
    return 1;
}

int draw_map(void) {
    int curr_pos_x, curr_pos_y;
    int limit_x = map->X;
    int limit_y = map->Y;
    //
    for (curr_pos_y = 0; curr_pos_y < limit_y; ++curr_pos_y) {
        for (curr_pos_x = 0; curr_pos_x < limit_x; ++curr_pos_x) {
            printf("%c", map->map_array[curr_pos_y][curr_pos_x]);
        }
        printf("\n");
    }
}

int main(void) {
    init_map_array();
    draw_map();
    //
    printf("STRUCT: %i\n", sizeof (map));
    printf("X: %i\n", sizeof (map->X));
    printf("Y: %i\n", sizeof (map->Y));
    printf("ARRAY: %i\n", sizeof (map->map_array));
return (EXIT_SUCCESS);
}

附带说明一下,最后的 4 个 printf 都返回“4”,我相当肯定,如果一个结构包含 3 个元素,每个元素都是 4 个字节,那么它本身应该大于 4 个字节...

【问题讨论】:

  • 结构体的大小为 12 字节;但是,您打印的是 MAP_s * 类型的地图大小,因此地图只有 4 个字节。

标签: c multidimensional-array malloc printf


【解决方案1】:

对我来说似乎工作正常,但有一个错误(不知何故,我的 GCC 足够“聪明”来处理它,但它仍然是一个错误):

map->map_array[curr_pos_y][curr_pos_x] = "#";

这分配了一个char *(指向数据段中的一个字符)而不是一个字符,这当然会导致奇怪的字符。将"#" 更改为'#',它应该可以工作。

另外,关于你最后的printfs:它们应该是这样的:

printf("STRUCT*: %lu\n", sizeof (map)); // Prints sizeof(MAP_s *) == sizeof(void *) == 4;
printf("STRUCT: %lu\n", sizeof (*map)); // Prints sizeof(MAP_s) == 16 on my system (iMac w/ Mac OS X),
        // alignment and native pointer size might give you different values.
printf("X: %d\n", map->X); // Prints the X dimension. No sizeof.
printf("Y: %d\n", map->Y); // Prints the Y dimension. No sizeof.

您无法打印map-&gt;map_array 的大小,因为sizeof 在编译时工作,并且只能返回编译时已知大小的类型的常量。确定map_array 大小的唯一方法是将您提供给malloc 的大小参数保存在一个变量中。

【讨论】:

    【解决方案2】:

    试试

    map->map_array[curr_pos_y][curr_pos_x] = '#';
    

    而不是

    map->map_array[curr_pos_y][curr_pos_x] = "#";
    

    map-&gt;map_array[curr_pos_y][curr_pos_x]char 类型,您正在为其分配一个字符串常量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 2017-12-29
      • 1970-01-01
      • 2014-12-21
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多