Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将字符串插入到链接列表无效_C_Linked List - Fatal编程技术网

C 将字符串插入到链接列表无效

C 将字符串插入到链接列表无效,c,linked-list,C,Linked List,当我想在链表中插入字符串时,它会显示出来 将“char”传递给参数的整数到指针转换不兼容 类型为“const char*”;使用&[-Wint conversion]获取地址 在strcpy行中(当前->名称,名称1) 这是密码 node* new_node(char name1,float num) { node *current = malloc(sizeof(node)); if (current == NULL) return NULL; strcpy(current->

当我想在链表中插入字符串时,它会显示出来

将“char”传递给参数的整数到指针转换不兼容 类型为“const char*”;使用&[-Wint conversion]获取地址 在
strcpy行中(当前->名称,名称1)

这是密码

node* new_node(char name1,float num) {
  node *current = malloc(sizeof(node));
  if (current == NULL) return NULL;
  strcpy(current->name,name1);
  current->score = num;
  current->next = NULL;
  return current;
}

struct NODE {
    char name[40];
    float score;
    struct NODE *next;
};

有人能帮我吗,我搜索并尝试了很多方法,但都没有成功。

在你的函数定义中,
node*new\u node(char name1,float num)
你得到的是一个字符,你想要的是一个指向字符串实际存储位置的指针,所以把这个定义改为node*
new\u node(char*name1,float num)


也可以在您可能声明函数的位置进行更改,例如在
h
文件中或此文件的开头。

这似乎是一个家庭作业问题

无论如何,您的代码有几个问题,例如

  • 声明顺序错误(
    struct NODE
    在使用后声明)。这可能不在实际代码中,但如果是,请更正它
  • 使用了错误的类型。在函数
    new\u node
    中,您使用的是
    node
    而不是
    node
    。这本身会引发一个编译器错误
  • 对于C中的数组,使用copy by value不起作用。该函数采用类型为
    char
    的第一个参数。如果您只想复制一个字符,那么按照建议获取其地址。否则,将类型更改为
    char*

     strcpy(current->name, &name1); \\ if name1 is of type char
     strcpy(current->name, name1);  \\ if name1 is of type char*
    
  • 使用
    strcpy
    而不是
    strncpy
    。前者存在潜在的缓冲区溢出


char name1
-->
char*name1
(name是一个字符数组,不是单个的
char
)@Keine Lust:我这样做了,但显示了错误“新节点的冲突类型”为什么要使用
节点(上)和
节点(下)?更改h文件中的定义也更改这是什么编译器?在您可能声明函数的位置也更改,例如,在h文件中或在该文件的开头,以其他方式发布精确编译错误转储