Linked List in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Linked List in C

Im trying to create Linked list in c language and I have this unexpected result when I'm trying to add an element at the beginning of the List. Can you find my mistake? Here is my code: https://code.sololearn.com/c62w8vcJnb8R/?ref=app

29th Jun 2019, 4:32 PM
Lighton
Lighton - avatar
8 Answers
+ 8
David Carroll check out the number of comments in Gordie [#observer_status_only] 's code. I suppose they have a place in explaining code to someone who needs more understanding? What do you think?
30th Jun 2019, 5:17 AM
Sonic
Sonic - avatar
+ 7
Sonic The use of code comments in code used for instructional purposes or for a learner to preserve their thoughts is 100% appropriate. 😉👌
30th Jun 2019, 5:21 AM
David Carroll
David Carroll - avatar
+ 6
If you use 1 * you create a copy of the pointer. If you make it point somewhere else in the function, which you are doing here, it is not reflected outside the function. The 1st * is there for the type itself (node_t*) and the 2nd * is there for the address of the pointer. So no, it's either ** and & or * and return the head.
29th Jun 2019, 4:46 PM
Dennis
Dennis - avatar
+ 5
SPush(head, 14); should be SPush(&head, 14); Some people choose to return the head so you can avoid the & and the **.
29th Jun 2019, 4:40 PM
Dennis
Dennis - avatar
+ 4
Yea, that's what pointers do to you. Try not to overthink it. It's just an integer that uses different CPU instructions. You'll get the hang of it if you use it alot. :)
29th Jun 2019, 5:00 PM
Dennis
Dennis - avatar
+ 3
Can I simply change the ** to * and avoid the &, will be the save work done?
29th Jun 2019, 4:42 PM
Lighton
Lighton - avatar
+ 3
It's a little bit tricky to my mind...
29th Jun 2019, 4:58 PM
Lighton
Lighton - avatar
+ 3
I'm trying to understand them by writing every variable and every pointer on a paper and it's value and the variable it is pointing to.
29th Jun 2019, 5:13 PM
Lighton
Lighton - avatar