首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分配struct = struct时的分段故障

分配struct = struct时的分段故障
EN

Stack Overflow用户
提问于 2017-02-13 08:27:00
回答 2查看 1.2K关注 0票数 0

我正在尝试创建一个临时的“迭代器”结构,该结构被分配到“列表”的开头,然后通过检查iterator->next != NULL遍历该结构列表。我相信问题在iterator = start系列(35 & 70)中。

应用程序编译时没有任何问题,但是当我./应用程序时,会给我一个分段错误(内核转储)。

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct record
{
    int                accountno;
    char               name[25];
    char               address[80];
    struct record*     next;
};

int     addRecord (struct record **, int, char [], char []);
void    printAllRecords(struct record *);

int main(int argc, char *argv[]) {
    struct record ** start;
    start = NULL;

    addRecord(start, 1, "Record Name", "Record Address");
    printAllRecords(*start);

    return 0;
}

void printAllRecords(struct record * start)
{
    struct record * recordIterator;

    /* Allocate the required memory and return a pointer to it */
    recordIterator = malloc(sizeof(struct record));

    /* Start at the beginning */
    recordIterator = start;

    printf("\n\n%10s %20s %20s\n", "accountno", "Name", "Address");

    while (recordIterator != NULL)
    {
        printf("%10d %20s %20s\n", recordIterator->accountno, recordIterator->name, recordIterator->address);
        recordIterator = recordIterator->next;
    }
}

int addRecord (struct record ** start, int accountno, char name[], char address[])
{
    struct record * newRecord;

    /* Allocate the required memory and return a pointer to it */
    newRecord = malloc(sizeof(struct record));

    /* Assign values to the new record */
    newRecord->accountno = accountno;
    strcpy(newRecord->name, name);
    strcpy(newRecord->address, address);

    if (start == NULL)
    {
        start = &newRecord;
    }
    else
    {
        struct record * recordIterator;

        /* Allocate the required memory and return a pointer to it */
        recordIterator = malloc(sizeof(struct record));

        /* Start at the beginning */
        recordIterator = *start;

        while (recordIterator->next != NULL)
        {
            recordIterator = recordIterator->next;
        }

        recordIterator->next = newRecord;
    }

    return 1;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-13 08:40:40

可以将start声明为指针,如

struct record * start;

然后可以通过addRecord(&start, ...)调用该方法。

在方法内部:

代码语言:javascript
复制
int addRecord (struct record ** start, int accountno, char name[], char address[])
{
    struct record * newRecord;

    /* Allocate the required memory and return a pointer to it */
    newRecord = malloc(sizeof(struct record));

    /* Assign values to the new record */
    newRecord->accountno = accountno;
    strcpy(newRecord->name, name);
    strcpy(newRecord->address, address);

    if (*start == NULL)
    {
        *start = newRecord;
    }

在函数中传递指针时,请记住,您可以永久修改的是--占用地址的值,而不是地址本身。在修改后的版本中,start的值不会更改(无论如何,我们都不能这样做.但是,我们正在修改start指向的值。

票数 4
EN

Stack Overflow用户

发布于 2017-02-13 08:31:47

这条线

代码语言:javascript
复制
addRecord(start, 1, "Record Name", "Record Address");

不会修改start。因此,当您调用start时,printAllRecords仍然是printAllRecords

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42199609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档