I am having troubles with my code. As you can see below, I have to make a list using linked list technique. My problem is that when I printf the s->item outside the add() function after performing it, the program crashes. When I print the s->item inside the add() function, it does print the correct data. Why does Statistician s become null again, even if the intialization of it is outside the while loop?
Please don't mind the typedefs, they are given by our instructor and we have to use it as basis so we dont have to change anything with the typedefs and structs.
typedef struct node *nodePtr;
struct node {
int item;
nodePtr next;
};
typedef nodePtr Statistician;
Statistician newStatistician(){
Statistician s = (Statistician)malloc(sizeof(Statistician));
s = NULL;
return s;
}
void add(Statistician s, int x){
Statistician newNode = (Statistician)malloc(sizeof(Statistician));
if(s == NULL){ // first node
printf("first");
newNode->next = NULL;
newNode->item = x;
s = newNode;
main(){
int menuChoice, clearDataChoice, x, outputInt, exitChoice, check;
float outputFloat;
Statistician s = newStatistician();
while (TRUE){
printf("\t\t*** STATISTICIAN PROGRAM v1 ***\n\n\n");
printf("Please enter data to be added : ");
x = inputNum();
add(s, x);
printf("%d", s->item);
//... bunch of other code
if(exitChoice==TRUE)
return 0;
else{
printf("\n\nPress any key to continue...");
getch();
system("cls");
fflush(stdin);
continue;
}
}
EDIT: add and main are 2 different functions
void add(Statistician s, int x){}
main(){}