I want to display all the topics (char *) from a linked list, but I'm getting a segmentation fault.
/* Scroll through the list and store the names in an array (topics)*/
char **get_topics() {
struct Topic *aux;
char **topics = NULL;
int i = 0;
aux = head;
while(aux != NULL) {
topics[i] = aux->name;
aux = aux->next;
i++;
}
return topics;
}
And then:
char **topics;
/* Get the topic names */
topics = get_topics();
/* Display all the topics */
while (*topics != NULL) {
printf("%s\n", *topics);
topics++;
}
topics?topics[i]doesn't magically create an array out of thin air. You may want to look up dynamic memory allocation.