hi sand_man,
i've already posted the code here
but here's a slightly different version to show my problem:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct charNode
{
double numberone;
double numbertwo;
struct charNode *next;
};
typedef struct charNode charNode;
void addElement(charNode **start, double numberone, double numbertwo);
void printList(charNode *start);
charNode* newNode(double numberone, double numbertwo);
void getString(charNode *start);
int main()
{
charNode *start = NULL;
getString(start);
printList(start);
return 0;
}
void getString(charNode *start)
{
addElement(&start, 1, 1);
addElement(&start, 2, 2);
addElement(&start, 3, 3);
printList(start);
}
void addElement(charNode **start, double numberone, double numbertwo)
{
charNode *temp,*prev, *loc;
temp = newNode(numberone, numbertwo);
if (*start == NULL)
{
*start = temp;
}
else
{
loc = *start;
prev = NULL;
while (loc != NULL)
{
prev = loc;
loc = loc->next;
}
temp->next = loc;
if (prev == NULL)
{
*start = temp;
}
else
{
prev->next = temp;
}
}
}
charNode* newNode(double numberone, double numbertwo)
{
charNode *temp;
temp = (charNode *) malloc(sizeof(charNode));
if (temp == NULL)
{
printf("WARNING - Memory allocation error\n");
exit(EXIT_FAILURE);
}
temp->numberone = numberone;
temp->numbertwo = numbertwo;
temp->next = NULL;
return temp;
}
void printList(charNode *start)
{
while (start != NULL)
{
printf("\n%f %f\n", start->numberone, start->numbertwo);
printf("%s %.2f %.2f %s", "next element is -+> ", start->next->numberone, start->next->numbertwo, "**OK**");
start = start->next;
}
/*
1.000000 1.000000
next element is -+> 2.00 2.00 **OK**
2.000000 2.000000
next element is -+> 3.00 3.00 **OK**
3.000000 3.000000
<--- BAM! segmentation fault.
notice how the "next element..." is not printed in the first line?