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

时间:2021-03-04 23:00:56

I have a program that basically adds a history(node) to the employee_record(linked list).

我有一个程序,基本上将一个历史(节点)添加到employee_record(链接列表)。

Here is my code:

这是我的代码:

#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");

}

However,when i key in a second entry,the department and title member of the previous history node gets replaced but not the day month and year as shown below 我的程序替换链表中所有节点中的所有字符串数据类型

但是,当我键入第二个条目时,前一个历史节点的部门和标题成员将被替换,但不会替换日期月份和年份,如下所示

Why is this so?

为什么会这样?

1 个解决方案

#1


2  

The scanf stores strings in variables a and b. The pointers to a and b are then passed to the addjob function. Then the addjob function copies the pointers into the structure. The structure just has a pointer to the buffers. It does not have a copy of the strings. The next time that scanf is called it overwrites the contents of the buffer, and the first strings are lost.

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

The solution is to make a copy of the strings, which you can do three ways

解决方案是制作字符串的副本,您可以通过三种方式完成

1) declare the struct as

1)将结构声明为

struct history{
   char department1[10];
   char title1[10];
   ...

and then use strcpy to copy the strings into the structure.

然后使用strcpy将字符串复制到结构中。

2) use strdup to make a duplicate of the strings

2)使用strdup来复制字符串

new->department1 = strdup(department);
new->title1 = strdup(title);

The issues with strdup: it's a non-standard function, and you have to free the memory when you're done with the strings.

strdup的问题:它是一个非标准函数,当你完成字符串时你必须释放内存。

3) use malloc and strcpy to duplicate the strings

3)使用malloc和strcpy复制字符串

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

This is slightly more work than strdup but only uses standard functions. You still have to free the memory when you're done with the strings.

这比strdup稍微多一些,但只使用标准函数。当你完成字符串时,你仍然需要释放内存。

#1


2  

The scanf stores strings in variables a and b. The pointers to a and b are then passed to the addjob function. Then the addjob function copies the pointers into the structure. The structure just has a pointer to the buffers. It does not have a copy of the strings. The next time that scanf is called it overwrites the contents of the buffer, and the first strings are lost.

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

The solution is to make a copy of the strings, which you can do three ways

解决方案是制作字符串的副本,您可以通过三种方式完成

1) declare the struct as

1)将结构声明为

struct history{
   char department1[10];
   char title1[10];
   ...

and then use strcpy to copy the strings into the structure.

然后使用strcpy将字符串复制到结构中。

2) use strdup to make a duplicate of the strings

2)使用strdup来复制字符串

new->department1 = strdup(department);
new->title1 = strdup(title);

The issues with strdup: it's a non-standard function, and you have to free the memory when you're done with the strings.

strdup的问题:它是一个非标准函数,当你完成字符串时你必须释放内存。

3) use malloc and strcpy to duplicate the strings

3)使用malloc和strcpy复制字符串

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

This is slightly more work than strdup but only uses standard functions. You still have to free the memory when you're done with the strings.

这比strdup稍微多一些,但只使用标准函数。当你完成字符串时,你仍然需要释放内存。