还是直接从代码看问题吧,谢谢各位的时间。
#include <stdio.h>
#include <string.h>
int lookup_keyword(char const * const desired_word, char const * keyword_table[]);
char const *keyword[] = {
"do",
"what",
"ever",
"anything",
"you",
"want",
NULL
};
int main(void)
{
int lenth = lookup_keyword("do", keyword); //这里main中调用函数是否正确呢,特别是第一个参数?
printf("%d\n", lenth);
return 0;
}
int lookup_keyword(char const * const desired_word, char const * keyword_table[])
{
char const **kwp;
for(kwp= keyword_table; *kwp != NULL; kwp++)
if (strcmp(desired_word, *kwp) == 0)
return (*kwp - desired_word);
return -1; //为什么书中对于for循环不加上大括号{}来包含代码块呢,所以在这里想问的是**什么情况下可以不用加大括号(指的是代码语句在二条及以上的时候)**
}
是对的。第一个参数char const* const是指向常量字符的
常量指针。可以接收char*, const char*, char const* const
类型参数。"str"会被作为const char*代入参数
if,for后面接单条语句,
{}
内的语句可视为单条语句。你不想让if或者for影响的语句就不用放在
{}
内了。