3 回答
In the code you posted you are attempting to format a string %s
as a result the printf method expects a char* to be passed. if you pass it an int or a single char it will issue a warning as it does.
to print a single char as per your example you would need:
printf("%c\n", map[0][0]);
The reason it works when you pass in the &map[0][0]
is because you are now passing in a pointer to a character ( char*
) which is the same as passing it an array of characters.
See the printf reference.
它之所以有效,是因为您发送了第一个元素(char *)类型的地址。该参数在 printf 函数中得到 DECAYED,该函数在您的情况下需要一个指向字符数组的 char 指针
由于 char 本身是一个数值,一个 8 位整数,这样的代码会给你这个警告。至于您的第二个问题,使用“address of”运算符获取 map[0][0] 的地址会为您提供指向 map[0] 的第一个元素的指针,或者在这种情况下为值 1,并且 printf 需要一个指向 char 数组的指针。考虑一下:
char map [20];
map [0]=1;
printf ("%s\n", map); //or printf ("%s\n", &map[0])
两者都指向第一个元素,有效地打印直到数组中到达 null,'\0'。
编辑:关于您的编辑,您仍在访问第一个元素,它是字符类型,本质上是数字。“X”为您提供该字符的数字等价物。