Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Struct - Fatal编程技术网

C 向结构中添加字符

C 向结构中添加字符,c,string,struct,C,String,Struct,我正在尝试将字符读入一个链表,我编写了这个简单的测试代码,只是为了尝试读入字符,因为某些原因,我无法读入字符值 #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char name[50]; struct node *next; }*head; void add(char AddName); int main() { head = N

我正在尝试将字符读入一个链表,我编写了这个简单的测试代码,只是为了尝试读入字符,因为某些原因,我无法读入字符值

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    char name[50];
    struct node *next;
}*head;

void add(char AddName);

int main()
{

    head = NULL;
    char TempName[50];

    printf("What Name");
         scanf(" %s", TempName);

    add(TempName);

    printf("%s",head->name);


    return 0;
 }


void add(char AddName)
{
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    strcpy(temp->name,AddName);
    head = temp;
    head->next = NULL;
}

我知道这不是链表的工作原理,我做这个只是为了能够在结构中运行单个字符的名称并将其打印出来。我应该能够输入名称Bob并打印Bob,我认为您的函数参数定义是错误的。试试这个:

void add(char *AddName)
{
....
}

编译器应该就代码向您发出警告。阅读警告,它们通常与错误一样重要。