EDIT: I got a little carried away with this (it's a slow day at work), so I rewrote a function to use (IMHO) more clear variable names, fewer redundant variables, and added some basic error handling. The example below supports insertion, while the previous example assumed a simple addition to the end of the list, which was the result of a misreading of the question (see Editing if you are interested).
void listAdd(Node* currentNode, Node toAdd) { Node * newNode = malloc(sizeof(Node)); if(!newNode){
As mentioned in previous posts. dereferencing a pointer to a structure element uses a pretty pretty note -> , which has pretty good images. Also note that NULL will always be evaluated as false, and if you do not want any bad things to happen (at best, segfault, at worst, one of you takes your computer), you need to make sure that you write to the appropriate memory area, so you should always check that malloc returns !NULL .
note: In C, never use the return value of a malloc() call, as this may mask strange and dangerous behavior. In C ++, you must specify the result, so you need to think about who you are going to offend if you expect your program to compile as valid C and C ++. See more about malloc results? .
source share