Hello
This piece of code is supposed to print X but doesn't . It crashes. Any help in identifying the problem would be greatly appreciated !
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct W {
char c;
struct W *next;
} list_node;
list_node *insert_list (char c, list_node *next_node) {
list_node *temp_node = malloc(sizeof(list_node));
temp_node->c = c;
temp_node->next = next_node;
return temp_node;
}
void read_input (list_node *list) {
char c = 'X';
list = insert_list(c, list);
}
int main (void) {
list_node *list = NULL;
read_input(list);
printf("%c", list->c);
return 0;
}
However, if I change line 27 to
Code:
list = read_input(list);
and change the read_input function to:
Code:
list_node *read_input (list_node *list) {
char c = 'X';
list = insert_list(c, list);
return list;
}
then it does print X. But I do not want to return a value back to main(), I want it to pass by reference like the first example.
Any suggestions?