程序中的未知错误

Unknown Errors in Program

我是 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 需要一个指针,但您已将一个整数传递给它。即使这是一个警告而不是错误(在我看来这应该是一个错误)你一定要在继续之前修复它。

  • note: expected ‘const char *’ but argument is of type ‘char’

    这显示了 strcmp 参数的预期类型和提供类型。您传递了一个 char 变量((*head_ref)->datanew_node->data)但是 strcmp 期望 const char*,一个指向字符的指针(在 [=12 的情况下基本上是一个字符串) =])

  • error: ‘current’ undeclared (first use in this function)
    error: ‘next’ undeclared (first use in this function)

    您尚未在此函数中声明 currentnext

  • warning: variable ‘temp’ set but not used [-Wunused-but-set-variable] struct node* temp;

    变量 temp 已被赋值,但未被使用。这基本上是在报告未使用的变量,您可以忽略此警告

  • error: unknown type name ‘Struct’; did you mean ‘struct’?

    错误是self-explanatory,甚至提供了解决方案。 C 是 case-sensitive.

  • 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-terminator [=35=] +1)。有一些方法可以使大小动态变化,但这是另一天的话题:-)