A char[ROWS][COLS+1]
cannot be cast into a char**
. The input argument of print_map
should be
void print_map(char map[][COLS+1])
or
void print_map(char (*map)[COLS+1])
The difference being that a char**
means to point to something that can be dereferenced like this:
(char**)map
|
v
+--------+--------+------+--------+-- ...
| 0x1200 | 0x1238 | NULL | 0x1200 |
+----|---+----|---+--|---+----|---+-- ...
v | = |
+-------+ | |
| "foo" | <-----------------'
+-------+ |
v
+---------------+
| "hello world" |
+---------------+
While a char(*)[n]
is a points to a continuous memory region like this
(char(*)[5])map
|
v
+-----------+---------+---------+-------------+-- ...
| "foo" | "hello" | " worl" | "d" |
+-----------+---------+---------+-------------+-- ...
If you treat a (char(*)[5])
as a (char**)
you get garbage:
(char**)map
|
v
+-----------+---------+---------+-------------+-- ...
| "foo" | "hello" | " worl" | "d" |
+-----------+---------+---------+-------------+-- ...
force cast (char[5]) into (char*):
+----------+------------+------------+------------+-- ...
| 0x6f6f66 | 0x6c686500 | 0x77206f6c | 0x646c726f |
+----|-----+---------|--+------|-----+------|-----+-- ...
v | | |
+---------------+ | | v
| "hsd?y?a?~22" | | | launch a missile
+---------------+ | |
v v
none of your process memory
SEGFAULT
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…