c - Infinite "Signal 11 being dropped" loop with Valgrind while trying to add to linked list -
i'm trying create simple singly linked list in c, , have encountered infinite "singal 11 being dropped" loop while running program in valgrind.
my .h file:
#ifndef test_h #define test_h struct fruit { char name[20]; }; struct node { struct fruit * data; struct node * next; }; struct list { struct node * header; unsigned count; }; #endif
my .c file:
#include "test.h" #include <stdio.h> #include <string.h> void init_list(struct list my_list) { my_list.header = null; my_list.count = 0; } void add_to_list(struct list my_list, struct fruit my_fruit) { struct node my_node; struct node nav_node; my_node.data = &my_fruit; my_node.next = null; if(my_list.count == 0) { /* set head node if list empty */ my_list.header = &my_node; my_list.count++; } else { nav_node = *my_list.header; while (nav_node.next != null) { /* traverse list until end */ nav_node = *nav_node.next; } nav_node.next = &my_node; my_list.count++; } } int main() { struct fruit fruit_array[5]; struct list fruit_list; int i; strcpy(fruit_array[0].name, "apple"); strcpy(fruit_array[1].name, "mango"); strcpy(fruit_array[2].name, "banana"); strcpy(fruit_array[3].name, "pear"); strcpy(fruit_array[4].name, "orange"); init_list(fruit_list); for(i=0; < 5; i++) { add_to_list(fruit_list, fruit_array[i]); } return 0; }
i'm assuming issue stems list traversal in add_to_list
, i'm unsure i'm doing wrong.
thanks!
you're passing structs value functions. create copy of struct in function, , changes copy not occur on struct in calling function.
you should read pointers in favorite c-language book.
Comments
Post a Comment