I am having trouble with the code below, for some reason there are no recipients in my linked list. I think it's a pointer problem. Also in my rcpt_add function is it best to return the head of the list? The problem I see with that is telling if the malloc in the function failed for the new node. Any suggestions would be appreciated, thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct rcptnode {
char data[16];
struct rcptnode *next;
} rcpt;
rcpt *rcpt_add(rcpt *node, char *value) {
rcpt *tmp;
rcpt *new_node;
new_node = malloc(sizeof(rcpt));
if(!new_node) {
return NULL;
}
new_node->next = NULL;
strncpy(new_node->data, value, 15);
if(!node) {
node = new_node;
} else {
tmp = node;
while(tmp->next) {
tmp->next = new_node;
}
}
return new_node;
}
void rcpt_list(rcpt *node) {
if(node) {
printf("Recipients:\n");
} else {
printf("No recipients\n");
}
while(node) {
if(node->data) {
printf("Data: %s\n", node->data);
}
node = node->next;
}
}
int main() {
rcpt *tmp = NULL;
rcpt_add(tmp, "test 1");
rcpt_add(tmp, "test 2");
rcpt_list(tmp);
return 0;
}