CS50 Pset 5(拼写器):变量初始化为 char 或 char*?
我已经完成了 CS50 PS5(拼写器)。除了加载函数中的一行代码之外,我理解该程序。语法看起来很简单,但我想了解发生了什么。
bool load(const char *dictionary)
{
// Open dictionary
FILE *d_pointer = fopen(dictionary, "r");
if (d_pointer == NULL)
{
printf("Unable to open %s\n.", dictionary);
return false;
}
// Variable for scanned words
char w_scan[LENGTH + 1];
// Scan words from dictionary
while (fscanf(d_pointer, "%s", w_scan) != EOF)
{
// Create new node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
// Copy word into node
strcpy(n->word, w_scan);
// Hash word
int h_value = hash(w_scan);
// Insert node into hash table
n->next = table[h_value];
table[h_value] = n;
d_size++;
}
// Close dictionary
fclose(d_pointer);
return true;
}
我的问题是,为什么表示扫描单词(w_scan)的变量必须初始化为字符而不是字符串?如果我将 w_scan 的初始化替换为 char *w_scan 而不是 char w_scan,我会收到以下错误:
dictionary.c:83:36: error: format specifies type 'char *' but the argument has type 'char **' [-Werror,-Wformat] while (fscanf(d_pointer, "%s", w_scan) != EOF)
对于基本问题抱歉。我显然在这里遗漏了一些东西,但我对我不明白的东西感到摸不着头脑。
I've completed CS50 PS5 (speller). I understand the program except for one line of code in the load function. The syntax seems trivial but I'd like to understand what's going on.
bool load(const char *dictionary)
{
// Open dictionary
FILE *d_pointer = fopen(dictionary, "r");
if (d_pointer == NULL)
{
printf("Unable to open %s\n.", dictionary);
return false;
}
// Variable for scanned words
char w_scan[LENGTH + 1];
// Scan words from dictionary
while (fscanf(d_pointer, "%s", w_scan) != EOF)
{
// Create new node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
// Copy word into node
strcpy(n->word, w_scan);
// Hash word
int h_value = hash(w_scan);
// Insert node into hash table
n->next = table[h_value];
table[h_value] = n;
d_size++;
}
// Close dictionary
fclose(d_pointer);
return true;
}
My questions is, why does the variable that represents scanned words (w_scan) have to be initialized as a char and not a string? If I replace the initialization of w_scan as char *w_scan instead of char w_scan I get the following error:
dictionary.c:83:36: error: format specifies type 'char *' but the argument has type 'char **' [-Werror,-Wformat] while (fscanf(d_pointer, "%s", w_scan) != EOF)
Sorry for the basic question. I'm obviously missing something here but I'm scratching my head as to what I'm not understanding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它没有被初始化为
char
。它被初始化为char
的数组,这正是保存字符串的对象类型。这让我想到了第二个要点:数据类型char *
不代表字符串。字符串是一个或多个char
的序列,最多包含一个值为 0 的终止符。char *
类型的对象对于引用字符串很有用,但它们不得与琴弦本身相混淆。这是区分指针和它所指向的事物的范畴。如果您改为编写,
那么您将声明一个指针数组,而不是一个
char
数组。在某些情况下,这可能正是您想要的,但这不是其中之一。It is not being initialized as a
char
. It is being initialized as an array ofchar
, which is exactly the type of object that holds a string. Which brings me to a second important point: data typechar *
does not represent a string. A string is a sequence of one or morechar
s, up to and including a terminator with value 0. Objects of typechar *
are useful for referring to strings, but they must not be confused with the strings themselves. This is in the category of distinguishing between the pointer and the thing to which it points.If you instead write
then you thereby declare an array of pointers, not an array of
char
. That might in some cases be exactly what you want, but this is not one of those cases.在此声明中,
声明了一个由
LENGTH + 1
字符组成的数组,用于通过此调用从循环中的文件中读取单词。该声明
没有任何意义。该函数读取字符序列而不是指针序列。
In this declaration
there is declared an array of
LENGTH + 1
characters that is used to read words from a file in a loop by means of this callThis declaration
does not make a sense. The function reads sequences of characters not sequences of pointers.