Hi all.
I am getting errors, but I can't understand. h is the address of a pointer, and I am returning the address of a pointer, so why are the types incompatible? Also, is it better to create new links recursively, or iteratively? Thanks.
Code:
main.c||In function ‘main’:|
main.c|16|error: incompatible types when assigning to type ‘struct node *’ from type ‘node’|
main.c|14|warning: variable ‘h’ set but not used [-Wunused-but-set-variable]|
main.c||In function ‘get_list’:|
main.c|34|error: incompatible types when assigning to type ‘struct element *’ from type ‘node’|
main.c|36|error: incompatible types when returning type ‘struct node *’ but ‘node’ was expected|
main.c|37|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 3 errors, 2 warnings ===|
Code:
// linkedlist.c: creates a linked list from a string, counts the number of nodes, and prints the list
// 3 functions: get_list(), count_list(), print_list()
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
node get_list(char s[]);
int count_list();
void printlist();
int main(void)
{
node *h;
h = get_list("ABC");
return 0;
}
node get_list(char s[])
{
node *h;
if(s[0] == '\0')
{
puts("Empty string");
exit(1);
}
else
{
h = malloc(sizeof(node));
h->data = s[0];
h->next = get_list(s + 1);
}
return h;
}
list.h
Code:
struct element {
char data;
struct element *next;
};
typedef struct element node;