2013-07-18 29 views
1

我是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)中的值很好。

感謝您提供的任何幫助。

回答

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

但是,請記住在適當的時候釋放內存。

+0

感謝您的幫助。 – Boardy