【问题标题】:Need help in pure c list implementation在纯 c 列表实现中需要帮助
【发布时间】:2013-05-25 18:00:36
【问题描述】:

我正在尝试在 c 中创建人员链接列表。 我所有的方法都在main() 中工作,直到我将它们放入while 循环(用于读取用户的命令)。一切都可以编译,但是当我尝试运行它时,它会崩溃返回随机值。 这是我的部分代码。

结构:

struct Person{
             const char* name;
             const char* sex;
             int age;
             struct Person* next;
} *head;

方法插入:

void insert(struct Person* h, char*n, char* s, int a){

    for(; h->next != NULL; h=h->next){}

    struct Person* p = (struct Person*) malloc(sizeof(struct Person)); 
    p->name=n;
    p->age=a;
    p->sex=s;
    p->next=NULL;
    h->next=p;
}

以及它不起作用的主要:

int main()
{
    struct Person Maciek={"Maciek", "Male", 20, NULL};
    head = &Maciek;
    int comand = 0;


    while(comand != 6){
        printf("Choose command:\n 1-insert person \n 2-delete by index \n 3-delete by name \n 4-display by index \n 5-print whole list \n 6-exit\n");
        scanf("%d", &comand);
        if(comand == 1){
            printf("Name, Gender, Age\n");
            char* name;
            char* sex;
            int age;            
            scanf("%s, %s, %d", &name, &sex, &age);
            printf("Name %s, Sex %s, Age %d", name, sex, age);

            insert(head, name, sex, age);
        }

        if(comand == 2){
            printf("2\n");
        }

        if(comand == 3){
            printf("3\n");
        }

        if(comand == 4){
            printf("4\n");
        }

        if(comand == 5){
            printf("5\n");
        }

    }

     return 0;
}

我是 C/C++ 的新手,如果能提供任何帮助,我将不胜感激。

【问题讨论】:

  • 你试过调试器吗?您无需在 C 程序中强制转换 malloc 的返回值。
  • 打开你的警告设置——至少你应该得到格式不匹配的警告。

标签: c list structure printf scanf


【解决方案1】:
    if(comand == 1){
        printf("Name, Gender, Age\n");
        char* name;
        char* sex;
        int age;            
        scanf("%s, %s, %d", &name, &sex, &age);

在这里,您使用的是悬空指针(指向内存中的任何位置),您应该使用 malloc 分配一些内存或使用 char 数组,正如 Carl Norum 指出的那样,您的 @ 中不应该有 & 987654324@ 致电,因为您需要提供一些 char* 而不是 char**。你可以这样做(这段代码容易受到缓冲区溢出的影响,不要在生产代码中使用它,考虑使用fgets+sscanf):

char name[50];
char sex[20];
int age = 0;
scanf("%s, %s, %d", name, sex, &age);

在你的插入函数中:

 struct Person* p = (struct Person*) malloc(sizeof(struct Person)); 
 p->name=n;
 p->age=a;
 p->sex=s;

您将 p->name 替换为 n,而不是将 n 的内容复制到 p->name 中。你想要:

struct Person *p = malloc(sizeof(struct Person));
p->name = malloc(strlen(n)+1);
if(p->name == NULL) {
  //error handling...
}
strcpy(p->name, n);
p->sex = malloc(strlen(s)+1);
if(p->sex == NULL) {
  //error handling...
}
strcpy(p->sex, s);
p->age = a;

【讨论】:

  • nameagescanf 调用中也不应该有 &
  • char* name =(char*) malloc(20*sizeof(char)); char* 性别 =(char*) malloc(6*sizeof(char));我已经改正了,还是不行,你能再帮帮我吗?
  • 删除 scanf 调用中的 & 运算符。而且您不需要在 C 中强制转换 malloc 的返回,您只需要在 C++ 中。 char *name = malloc(20*sizeof(char));
  • char *name = malloc(20) 很好。 sizeof(char)1
  • 使用strdup()比较明智,或者在分配之前测量字符串的长度(记得为'\0'加1)。
【解决方案2】:

您正在将字符串读取到尚未使用分配的内存初始化的指针中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2023-01-26
    相关资源
    最近更新 更多