i have a getString method which runs addElement and adds stuff to the linklist. Add element, finds the actual position in the linklist i.e. if its going at the start or middle etc so it runs newNode. My problem is
when run my printList method within getString, the list is printed out fine. If i attempt to print it out in the main method a blank result is printed. For some reason the list contents are erased (i.e. the main method has no idea what happened). My code is below, i've kept it as minimal as possible:
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); <-- does not. return 0; } void getString(charNode *start) { addElement(&start, 11.2, 33.3); addElement(&start, 12.3, 11.3); addElement(&start, 122.3, 11.3); printList(start); //<-- works } 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("%f %f\n", start->numberone, start->numbertwo); start = start->next; } }