我是C语言的新手,来自Java背景,调试这些函数时遇到了麻烦。基本上,他们的想法是传递一个单词,为链接列表创建一个节点,然后将新节点排序插入到链接列表中。我不明白我的错误在代码中,并且我在寻找如何解决它们方面遇到困难。如果有人可以帮助引导我走上正确的道路,我将非常感谢您的帮助!谢谢大家!请在下面找到我的函数的代码。
#include <stdio.h>
#include <string.h>
//Node record
struct node {
char data;
struct node* next;
};
//insertion sorting (will sort as inserted)
void insert_dictionary_order(struct node** head_ref, char word)
{
struct node* temp;
struct node* new_node;
new_node->data = word;
//If new insert is before head, put current head next, and set head to new node
if (*head_ref == NULL || strcmp((*head_ref)->data, new_node->data) > 0) //if new_node comes before this
{
new_node->next=*head_ref;
*head_ref = new_node;
}
else
{
//find the node before insertion point
temp = *head_ref;
while(current->next != NULL && current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = next;
current->next = new_node;
}
}
//function to print list by being passed head
void print_list(struct node *head)
{
Struct node *temp = head;
while(temp != NULL)
{
printf("%s\n", temp->data);
temp = temp->next;
}
}
这是我收到的错误:17899186/source.c: In function ‘insert_dictionary_order’:
17899186/source.c:21:36: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
if (*head_ref == NULL || strcmp((*head_ref)->data, new_node->data) > 0) //if new_node comes before this
^
In file included from 17899186/source.c:4:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
17899186/source.c:21:55: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
if (*head_ref == NULL || strcmp((*head_ref)->data, new_node->data) > 0) //if new_node comes before this
^~~~~~~~
In file included from 17899186/source.c:4:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
17899186/source.c:30:14: error: ‘current’ undeclared (first use in this function)
while(current->next != NULL && current->next->data < new_node->data)
^~~~~~~
17899186/source.c:30:14: note: each undeclared identifier is reported only once for each function it appears in
17899186/source.c:34:25: error: ‘next’ undeclared (first use in this function)
new_node->next = next;
^~~~
17899186/source.c:16:17: warning: variable ‘temp’ set but not used [-Wunused-but-set-variable]
struct node* temp;
^~~~
17899186/source.c: In function ‘print_list’:
17899186/source.c:45:5: error: unknown type name ‘Struct’; did you mean ‘struct’?
Struct node *temp = head;
^~~~~~
struct
17899186/source.c:45:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
Struct node *temp = head;
^
17899186/source.c:46:11: error: ‘temp’ undeclared (first use in this function); did you mean ‘bcmp’?
while(temp != NULL)
^~~~
bcmp
最佳答案
好吧,让我们仔细看看您得到的错误和警告:
warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
这些警告说strcmp
需要一个指针,但是您已经给它传递了一个整数。即使这是一个警告,而不是一个错误(这应该是IMO错误),您仍然应该在继续操作之前进行修复。note: expected ‘const char *’ but argument is of type ‘char’
这显示了strcmp
参数的预期类型和提供的类型。您传递了char
变量((*head_ref)->data
和new_node->data
),但是strcmp
期望使用const char*
,它是指向字符的指针(对于strcmp
,基本上是字符串)error: ‘current’ undeclared (first use in this function)
error: ‘next’ undeclared (first use in this function)
您尚未在此函数中声明current
和next
。warning: variable ‘temp’ set but not used [-Wunused-but-set-variable] struct node* temp;
变量temp
被分配了一个值,但未使用。这基本上是报告未使用的变量,您可以忽略此警告error: unknown type name ‘Struct’; did you mean ‘struct’?
错误是不言自明的,甚至提供了解决方案。 C区分大小写。error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
error: ‘temp’ undeclared (first use in this function); did you mean ‘bcmp’?
由于前一个错误而引发了这些错误。修复拼写错误Struct
-> struct
时,这些应该会消失。您应该注意
char data;
可以包含单个字符,不能包含整个字符串。如果需要存储字符串,则必须使用char data[64];
这样的数组。 64表示数组的大小。这意味着data
最多可以存储63个字符(NUL终止符\0
的末尾为+1)。有多种方法可以使大小动态变化,但这是另一天的主题:-)关于c - 程序中的未知错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64745618/