+ 1
Linked List not working properly (C)
I'm doing a linked list but when it comes to printing it the output is not correct, the code should create a node and give it a value of 3, then put 5 in head of the list and move 3 next. https://code.sololearn.com/chhCSZCClOIb/#c
4 Respuestas
+ 1
Guido Parlatore This code is corrected, please check it.
https://code.sololearn.com/c7ahQn7MB6Hr/?ref=app
You tried to edit the pointer inside "listain0", but the pointer value (the memory address it points to) is passed by value. Once you exit the function, any assignments to "a" are lost.
To make it work, the pointer must be passed "by reference", which in C is simulated by passing a pointer. This makes necessary to pass a pointer of a pointer (double pointer).
I also corrected a minor bug on the function---it was expecting to return a node, but it wasn't, so I made it void.
+ 1
Guido Parlatore Indeed. I'm checking your code to see if I spot any bugs.
0
In the function "newdinarray" make sure you return the node you're creating.
return a;
0
I did and it is still not working, you mean adding at the end of the function return a; right?