所以,我正在研究这个hang子手游戏。我在数组和指针方面有点麻烦。这是我有:在C函数中返回一个数组中的项目
char* getword(int index);
int main (void) {
char *secword = getword(1);
printf("%s ", *secword);
}
char *getword(int index)
{
char *words[8]= {'bye', 'hi','what', 'cat', 'dog',
'bird', 'air', 'water', 'fire'};
return words[index];
}
我不断收到一个分段错误(核心转储)错误。
C中的字符串使用双引号。该阵列中还有9个字符串,而不是8个。 – 2015-03-08 21:05:03
是的 - 你是如何设法达到段错误的? – 2015-03-08 21:06:28
使用'char const *'而不是'char *',并且您需要使用'printf(“%s”,secword);'或'printf(“%c”,* secword);'。并注意编译器消息;如果有任何警告或错误,甚至不打扰你的程序运行 - 首先解决错误。 – 2015-03-08 21:06:59