所以我得到了一个我从教科书中输入的程序。
#include <stdio.h>
#define string char*
int main(void) {
string a[] = { "I", "like", "to", "fight," },
b[] = {"pinch,","and","bite."};
printf("%s %s %s %s %s %s %s\n", a[0], a[1], a[2], a[3], b[0], b[1], b[2]);
return 0;
}
目前它会引发以下错误:
exit status 1
main.c: In function 'main':
main.c:7:18: error: excess elements in char array initializer
b[] = {"pinch,","and","bite."};
main.c:7:18: note: (near initialization for 'b')
main.c:7:24: error: excess elements in char array initializer
b[] = {"pinch,","and","bite."};
main.c:7:24: note: (near initialization for 'b')
显然,目标是让这项工作成功。该书暗示,在#define
预处理器中,可以添加一个单个字符以使其工作。并且,由于错误都以b
字符串为中心,我认为它是b
。但事实并非如此。不管怎样,或者我把b
置于错误的位置。我对这个问题的理解是,虽然它为字符串创建了足够的空间,但它不适用于b
字符串。
任何意见都会受到赞赏。
答案 0 :(得分:3)
好吧,如果你有
int *a, b;
你很快就会发现你有一个int指针和一个int。您的代码也会发生同样的事情
string a[] = { "I", "like", "to", "fight," },
b[] = {"pinch,","and","bite."};
变为
char* a[] = { "I", "like", "to", "fight," },
b[] = {"pinch,","and","bite."};
所以你有一个char指针数组和一个char数组。您需要添加的单个char是*
,以使b
成为一个char指针数组。
char* a[] = { "I", "like", "to", "fight," },
*b[] = {"pinch,","and","bite."};