c - 在 C 中的列表末尾插入项目,产生段错误

标签 c segmentation-fault

我试图在列表的末尾插入项目,但是,当我编译它时,它确实为 Record ptr 分配了内存,但它没有在列表的末尾插入项目。段错误。有人可以帮忙吗?干杯。

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


/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
      "Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};


/* declare your struct for a person here */
typedef struct Record{
    char *name;
    int age;
    struct Record *next;
}  Record;

//set the head pointer at the start of the list
Record *headptr = NULL;

static void insert (Record *p, char *s, int n) {

/* create a new space for the new person */
Record *ptr =(Record*) malloc(sizeof(Record));

    /* check if it is succeeded  */ 
    if(ptr == NULL){  
        abort();
        printf("memory allocation fail"); 
        exit(1);  
    }else{
        printf("memory allocation to person  - %s - \n", s);      
    }

    //set the data for the new person
    ptr->name=s;
    ptr->age=n;

    //ptr= NULL; 

    if(headptr==NULL){
        headptr = ptr->next;
        ptr=headptr;

    }else{
        while( ptr->next == NULL) {
            ptr=ptr->next;
        headptr=ptr;
        }
    }  
}  

int main(int argc, char **argv) {

    /* declare the people array here */
    Record *p=headptr;
    headptr = NULL;

    //insert the members and age into the unusage array. 
    for (int i=0; i < 7; i++) {
        insert (p,names[i], ages[i]);

    /* do not dereference the pointer */
    }

    /* print out a line before printing the names and ages */
    printf("\n");

    //set the pointer at the start of the list 
    p = headptr;

    /* print the people array here*/
    for (int i = 0; i < 7; i++, p = p->next) {
        printf("The name is: %s, the age is:%i\n", p->name, p->age);
    }


     /* This is the third loop for call free to release the memory allocated by malloc */
     /* the free()function deallocate the space pointed by ptr. */
     for(int i=0; i<7;i++){
         free(p);
     }

}

最佳答案

您的 insert() 函数有多个错误:

static void insert (Record *p, char *s, int n) {

    /* create a new space for the new person */
    Record *ptr =(Record*) malloc(sizeof(Record));

    /* check if it is succeeded  */ 
    if(ptr == NULL){  
        abort();
        printf("memory allocation fail"); 
        exit(1);  
    }else{
        printf("memory allocation to person  - %s - \n", s);      
    }

    //set the data for the new person
    ptr->name=s;
    ptr->age=n;
    ptr->next=NULL;

    if(headptr==NULL)
    {
        headptr = ptr;
    }else{
        // Iterate over complete list until the last element has been reached.
        Record* tail = headptr;
        while(tail->next!=NULL)
        {
            tail=tail->next;
        }

        // Append new element to last element.
        tail->next = ptr;
    }  
}

但是,这个链表实现效率相当低,我建议您在继续编程之前阅读一下链表是如何在 C 中完成的:http://www.thegeekstuff.com/2012/08/c-linked-list-example/

关于c - 在 C 中的列表末尾插入项目,产生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13564647/

相关文章:

c - 简单的 curses.h 程序,终端不显示任何内容

c - 我想我以错误的方式声明和使用了数组

c++ - 使用 FastDelegate 的段错误

c - 检查 argv 中的符号时出现段错误

c - 由终端参数引起的段错误

c - 为什么从控制台读取字符并存储在数组中时出现段错误?

c - 当线程在已解锁的互斥锁上调用 pthread_mutex_unlock 时会发生什么

对 sizeof() 函数的结果感到困惑

c - 如何使用 autotools 构建架构/机器相关代码

c++ - SFML Sprite std::list