【问题标题】:Linked list within a structure结构内的链表
【发布时间】:2013-07-18 10:17:06
【问题描述】:

我是 C 新手,我正在尝试为我在一个小型测试应用程序中开发的程序制定逻辑。

它的目的是从数据库中读取值,将数据添加到结构中,但在此结构中将包含一个链接列表,指向与顶级结构相关的其他值。我似乎在结构和链表中添加了一些东西,但是当我尝试检索它崩溃的值时。

下面是我的结构的定义

typedef struct CallLogStructure
{
    char * date;
    char * time;
    char * bParty;
    char * aParty;
    float duration;
    char * cleardownCause;
    struct Node *outBoundLegs;
} callLogStructure;

typedef struct Node
{
    char * target;
    float targetDuration;
    char * targetCleardownCause;
    struct Node *next;
}node;

下面是我如何初始化结构,然后调用方法将数据添加到链表中。

char *outboundTarget = "0";
    float outboundDuration = 0;
    char *outboundCleardown = "0";

    callLogStructure * callLog = NULL;
    node *temp = NULL;
    int dataRow = 0;

    callLog = malloc(dataRow+1 * sizeof(callLog));
    //start = (node*)malloc(sizeof(node));
    callLog[0].outBoundLegs = NULL;
    callLog[0].outBoundLegs = (node*)malloc(sizeof(node));
    if (callLog[0].outBoundLegs == NULL)
    {
        printf("Failed to allocate RAM\n");
    }
    temp = &callLog[0].outBoundLegs;
    temp->next = NULL;
    callLog[0].outBoundLegs->target = "0";
    callLog[0].outBoundLegs->targetDuration = 0;
    callLog[0].outBoundLegs->targetCleardownCause = "0";

    //Insert first inbound leg
    callLog[0].date = "16/05/2011";
    callLog[0].time = "00:00:03";
    callLog[0].aParty = "12345";
    callLog[0].bParty = "67890";
    callLog[0].duration = 0;
    callLog[0].cleardownCause = "unanswered";

    outboundTarget = "98765";
    outboundDuration = 0;
    outboundCleardown = "Unanswered";

    insertOutBoundLeg(&callLog[0].outBoundLegs, outboundTarget, outboundDuration, outboundCleardown);


    printf("NEWLY INSERTED OUTBOUND TARGET: %s", callLog[0].outBoundLegs->target); //This is where it's crashing.

下面是insertOutBoundLeg函数

void insertOutBoundLeg(struct Node *pointer, char * target, float targetDuration, char * targetCleardownCause)
{
    if (pointer->target == NULL)
    {
        asprintf(&pointer->target, "%s", target);
        pointer->targetDuration = targetDuration;
        asprintf(&pointer->targetCleardownCause, "%s", targetCleardownCause);
        //pointer->target = target;
        //pointer->targetDuration = targetDuration;
        //pointer->targetCleardownCause = targetCleardownCause;
    }
    else
    {
        while (pointer->next != NULL)
        {
            pointer = pointer->next;
        }
        pointer->next = (node *)malloc(sizeof(node));
        pointer = pointer->next;
        //pointer->target = target;
        //pointer->targetDuration = targetDuration;
        //pointer->targetCleardownCause = targetCleardownCause;
        asprintf(&pointer->target, "%s", target);
        pointer->targetDuration = targetDuration;
        asprintf(&pointer->targetCleardownCause, "%s", targetCleardownCause);
        pointer->next = NULL;
    }
}

这个想法是,最终当它被构建时,结构以及包含在结构中的链表将被传递给一个单独的函数,该函数将数据导出到一个文件中,我已经尝试过首先打印值outboundlegs (链表),但这也会崩溃,但是,来自顶级结构(callLog)的值很好。

感谢您提供的任何帮助。

【问题讨论】:

    标签: c linked-list structure


    【解决方案1】:

    首先有多个问题

    callLog = malloc(dataRow+1 * sizeof(callLog));
    

    改成

    callLog = malloc(dataRow+1 * sizeof(*callLog));
    

    要么将callLog[0].outBoundLegs 初始化为0 为memset(callLog[0].outBoundLegs, 0, sizeof(*callLog[0].outBoundLegs)),要么使用calloc()

    callLog[0].outBoundLegs = calloc(1, sizeof(node));
    

    callLog[0].outBoundLegs->target = "0";
    

    不要那样初始化字符串,这样做

    callLog[0].outBoundLegs->target = strdup("0");
    

    但是,请记住在适当的时候释放内存。

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2015-12-27
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多