I am new to C and I am trying to work out the logic for a program I am working on in a small test app.
What the purpose is it will read values from a database which add data to a structure, but within this structure will contain a linked list to other values that are related to the top level structure. I seem to be adding things fine to the structure and the linked list but its then when I try and retrieve the values it crashes.
Below is the definition of my structures
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;
Below is how I am initialising the structures and then calling the method to add the data to the linked list.
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.
Below is the insertOutBoundLeg function
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;
}
}
The idea is that eventually when this is built, the structure, along with the linked list contained within the structure will be passed to a separate function which will export the data to a file, which I have tried by first printing the values of the outboundlegs (linked list) but this also crashes, however, the values from the top level structure (callLog) are fine.
Thanks for any help you can provide.