好吧我的C有点生疏,但我想我会在C中制作我的下一个(小)项目,所以我可以对它进行修改,不到20行,我已经有了一个seg错误. 这是我的完整代码: #define ROWS 4#define COLS 4char main_map[ROWS][COLS+
这是我的完整代码:
#define ROWS 4 #define COLS 4 char main_map[ROWS][COLS+1]={ "a.bb", "a.c.", "adc.", ".dc."}; void print_map(char** map){ int i; for(i=0;i<ROWS;i++){ puts(map[i]); //segfault here } } int main(){ print_map(main_map); //if I comment out this line it will work. puts(main_map[3]); return 0; }
我完全混淆了这是如何导致段错误的.从[] []转换为**时发生了什么?这是我得到的唯一警告.
rushhour.c:23:3: warning: passing argument 1 of ‘print_map’ from incompatible pointer type rushhour.c:13:7: note: expected ‘char **’ but argument is of type ‘char (*)[5]’
[] []和**真的不兼容指针类型吗?他们似乎只是我的语法.
char [ROWS] [COLS 1]无法转换为char **. print_map的输入参数应该是void print_map(char map[][COLS+1])
要么
void print_map(char (*map)[COLS+1])
区别在于char **意味着指向可以像这样解除引用的东西:
(char**)map | v +--------+--------+------+--------+-- ... | 0x1200 | 0x1238 | NULL | 0x1200 | +----|---+----|---+--|---+----|---+-- ... v | = | +-------+ | | | "foo" | <-----------------' +-------+ | v +---------------+ | "hello world" | +---------------+
虽然char(*)[n]是指向这样的连续存储区域
(char(*)[5])map | v +-----------+---------+---------+-------------+-- ... | "foo\0\0" | "hello" | " worl" | "d\0\0\0\0" | +-----------+---------+---------+-------------+-- ...
如果将(char(*)[5])视为(char **),则会产生垃圾:
(char**)map | v +-----------+---------+---------+-------------+-- ... | "foo\0\0" | "hello" | " worl" | "d\0\0\0\0" | +-----------+---------+---------+-------------+-- ... force cast (char[5]) into (char*): +----------+------------+------------+------------+-- ... | 0x6f6f66 | 0x6c686500 | 0x77206f6c | 0x646c726f | +----|-----+---------|--+------|-----+------|-----+-- ... v | | | +---------------+ | | v | "hsd®yœâñ~22" | | | launch a missile +---------------+ | | v v none of your process memory SEGFAULT