C 我的程序将替换链表中所有节点中的所有字符串数据类型

C 我的程序将替换链表中所有节点中的所有字符串数据类型,c,string,data-structures,linked-list,nodes,C,String,Data Structures,Linked List,Nodes,我有一个程序,基本上是将历史(节点)添加到员工记录(链表)中 这是我的密码: #include <stdio.h> #include <stdlib.h> struct history{ char *department1; char *title1; int day; int month; int year; struct history *next; }; struct emplo

我有一个程序,基本上是将历史(节点)添加到员工记录(链表)中

这是我的密码:

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

struct history{
       char *department1;
       char *title1;
       int day;
       int month;
       int year;
       struct history *next;
};

struct employee_record{
char firstname[20];
char lastname[20];
long int employee_id;
char sex;
int age;
struct history *head;
};

void addjob(struct employee_record *rec,
char *department, char *title,
int day, int month, int year);

void print(struct employee_record *rec);

int main(int argc, char *argv[])
{
  struct employee_record *worker=(struct employee_record*)malloc(sizeof(struct employee_record));
  worker->head=NULL;
  int c,d,e;
  printf("Department\tTitle\tDay\tMonth\tYear\n");
  while (1){
  char a[10]=" ";
  char b[10]=" ";
  scanf("%s %s %d %d %d",a,b,&c,&d,&e);
  addjob(worker,a,b,c,d,e);
  printf("Department\tTitle\tDay\tMonth\tYear\n");
  print(worker);
  }
  return 0;
}

void addjob(struct employee_record *rec,
char *department, char *title,
int day, int month, int year){
    struct history *new=(struct history*)malloc(sizeof(struct history));
    struct employee_record *temp;
    new->day=day;
    new->department1=department;
    new->month=month;
    new->year=year;
    new->title1=title;
    if (rec->head != NULL)
        new->next=rec->head;
    else {
        new->next=NULL;
    }
    rec->head=new;
}

void print(struct employee_record *rec){
    struct history *temp;
        temp=rec->head;
        printf("%s\t%s\t%d\t%d\t%d",temp->department1,temp->title1,temp->day,temp->month,temp->year);
        while(temp->next!=NULL){
            printf("\n");
            temp=temp->next;
            printf("%s\t%s\t%d\t%d\t%d",temp->department1,temp->title1,temp->day,temp->month,temp->year);
            }
        printf("\n");

}
#包括
#包括
结构历史{
char*部门1;
字符*标题1;
国际日;
整月;
国际年;
结构历史*下一步;
};
结构雇员记录{
charfirstname[20];
char lastname[20];
长整型员工id;
性别;
智力年龄;
结构历史*头;
};
void addjob(结构员工记录*rec,
字符*部门,字符*标题,
整数日、整数月、整数年);
作废打印(结构员工记录*rec);
int main(int argc,char*argv[])
{
struct employee_record*worker=(struct employee_record*)malloc(sizeof(struct employee_record));
worker->head=NULL;
int c,d,e;
printf(“Department\tTitle\tDay\tMonth\tYear\n”);
而(1){
字符a[10]=“”;
字符b[10]=“”;
scanf(“%s%s%d%d%d”,a、b、c、d、e);
addjob(工人a、b、c、d、e);
printf(“Department\tTitle\tDay\tMonth\tYear\n”);
印刷(工人);
}
返回0;
}
void addjob(结构员工记录*rec,
字符*部门,字符*标题,
整数日、整数月、整数年){
结构历史*新建=(结构历史*)malloc(sizeof(结构历史));
结构员工记录*临时;
新->天=天;
新建->部门1=部门;
新建->月份=月份;
新->年=年;
新建->标题1=标题;
如果(记录->头部!=NULL)
新建->下一步=记录->头部;
否则{
新建->下一步=空;
}
rec->head=新建;
}
作废打印(结构员工记录*rec){
结构历史*温度;
温度=记录->磁头;
printf(“%s\t%s\t%d\t%d\t%d”,临时->部门1,临时->标题1,临时->日,临时->月,临时->年);
while(临时->下一步!=NULL){
printf(“\n”);
温度=温度->下一步;
printf(“%s\t%s\t%d\t%d\t%d”,临时->部门1,临时->标题1,临时->日,临时->月,临时->年);
}
printf(“\n”);
}
但是,当我输入第二个条目时,会替换上一个历史节点的部门和标题成员,但不会替换日期、月份和年份,如下所示


为什么会这样?

scanf将字符串存储在变量
a
b
中。指向
a
b
的指针随后被传递到
addjob
函数。然后,
addjob
函数将指针复制到结构中。该结构只有一个指向缓冲区的指针。它没有字符串的副本。下次调用
scanf
时,它将覆盖缓冲区的内容,并且第一个字符串将丢失

解决方案是复制字符串,这有三种方法

1) 将结构声明为

struct history{
   char department1[10];
   char title1[10];
   ...
然后使用
strcpy
将字符串复制到结构中

2) 使用
strdup
复制字符串

new->department1 = strdup(department);
new->title1 = strdup(title);
new->department1 = malloc( strlen(department) + 1 );
strcpy( new->department1, department );
new->title1 = malloc( strlen(title) + 1 );
strcpy( new->title1, title );
strdup
的问题:这是一个非标准函数,处理完字符串后必须
释放内存

3) 使用
malloc
strcpy
复制字符串

new->department1 = strdup(department);
new->title1 = strdup(title);
new->department1 = malloc( strlen(department) + 1 );
strcpy( new->department1, department );
new->title1 = malloc( strlen(title) + 1 );
strcpy( new->title1, title );

这比strdup稍微多一些,但只使用标准函数。处理完字符串后,您仍然必须
释放内存。

new->department1=department意味着
新建->部门1=a(在主界面),所以您需要
new->department1=malloc(strlen(department)+1);strcpy(新->部门1,部门),与标题相同。