When I do:
t_node *node1;
t_node **head;
void *elem;
int c = 1;
elem = &c;
head = NULL;
node1 = malloc(sizeof(t_node));
node1->elem = elem;
head = &node1;
//add_node(node1,head); //substitute this with line above and it doesn't work
printf("%d\n",*(int*)(*head)->elem); // prints 1 and works fine
BUT WHEN I CREATE A FUNCTION CALLED ADD_NODE IT DOESN’T WORK?!??!?
void add_node(t_node *node1, t_node **head){
head = &node1;
}
this doesn’t make any sense to me… why would this cause this to not work? calling the function literally does the same exact code.
EDIT: Keep in mind the signature of the add_node is not under question. it is required of me to have that signature
C is a pass by value language. Your function actually doesn’t do anything at all as far as
main()is concerned.Check out this question in the comp.lang.c FAQ.