+ 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

20th Jun 2020, 6:52 PM
Guido Parlatore
Guido Parlatore - avatar
4 odpowiedzi
+ 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.
20th Jun 2020, 7:12 PM
Felipe BF
+ 1
Guido Parlatore Indeed. I'm checking your code to see if I spot any bugs.
20th Jun 2020, 7:08 PM
Felipe BF
0
In the function "newdinarray" make sure you return the node you're creating. return a;
20th Jun 2020, 7:03 PM
Felipe BF
0
I did and it is still not working, you mean adding at the end of the function return a; right?
20th Jun 2020, 7:07 PM
Guido Parlatore
Guido Parlatore - avatar