0
struct nodeStruct {
    int value;
    struct nodeStruct *next;
}

struct nodeStruct* List_createNode(int item) {
    nodeStruct* tempNode;
    tempNode->value = item;
    nodeStruct* node = malloc ( sizeof(tempNode) );
    return nodeStruct;
}

This is what I have so far, How would I allocate memory for a new node and initalize the value, then return a pointer to it? Not sure if I have the right approach.

Correction:

struct nodeStruct* List_createNode(int item) {
    struct nodeStruct node* = (struct nodeStruct *) malloc(sizeof(struct nodeStruct));
    if (node == NULL) {return NULL;}
    node->value = item;
    return node;
}

Would this return a pointer to a new node?

Aaron
  • 1,208
  • 1
  • 9
  • 21
  • 1
    You need to allocate the memory *before* you can write to it. Get rid of the `tempNode`, move the `malloc` up to the top and check its return value for `NULL`. Also look at your compiler's output. The above code should produce a whole pile of error messages. – 5gon12eder Jan 25 '15 at 04:49
  • You've got a bug in your return statement, return tempnnode, also in such a simple example, don't set tempnnode.value if tempnnode is NULL – phil Jan 25 '15 at 04:59
  • Don't implement all of @5gon12eder suggestions, you need tempnode – phil Jan 25 '15 at 05:02
  • @rhubarbdog: why? The variable `node` or the variable `tempNode` is needed, but not both. The code could be `struct nodeStruct *List_createNode(int item) { nodeStruct *node = malloc(sizeof(*node)); if (node != 0) node->value = item; return node; }`. You can spell `node` as `tempNode` consistently if you prefer, but the 'temp' appellation isn't accurate — the node survives longer than the function, so maybe `permNode` would be a better name (though `new_node` would be simple and accurate, or `newNode` in camelCase), but in a simple function like this, the unqualified name `node` is sufficient. – Jonathan Leffler Jan 25 '15 at 05:16
  • 1
    The edited version of the code is OK. There are those who will excoriate the use of the [cast on `malloc()`](http://stackoverflow.com/questions/605845/), but it's necessary if the code will be compiled by a C++ compiler rather than a C compiler. In my sample code, I wrote `nodeStruct *node = malloc(sizeof(*node));` which omits the cast (though I have plenty of my own code with casts because it is bilingual in C and the C subset of C++), and uses `sizeof(*node))` which adapts automatically to changes of type (unlike your proposed version which mentions the type 3 times on a single line). – Jonathan Leffler Jan 25 '15 at 05:34
  • I should also have observed that you need to set `node->next = NULL;` in the function, so that things don't go haywire if you try to follow the list starting at `node`. [Rawnish](http://stackoverflow.com/users/3911658/rawnish) did make an assignment to `node->next` in his [answer](http://stackoverflow.com/a/28134084/15168), pointing out the error (oversight) in my comment/answer. – Jonathan Leffler Jan 25 '15 at 08:15
  • @JonathanLeffler I get this error, `expected ‘;’, identifier or ‘(’ before ‘struct’ struct nodeStruct* List_createNode(int item) {` ^ – Aaron Jan 25 '15 at 08:18
  • 1
    There's a semicolon missing at the end of the structure definition in the code fragment in the question. Also, inside the function, the variable should be declared `struct nodeStruct *node` (you can only omit the `struct` if you're using C++, or if you create `typedef struct nodeStruct nodeStruct;`). That's correct in your code; it is incorrect in my comment — just as well it is a comment! – Jonathan Leffler Jan 25 '15 at 08:20

1 Answers1

-1
struct nodeStruct* List_createNode(int item) {
    struct nodeStruct node* = (struct nodeStruct *) malloc(sizeof(struct nodeStruct));
    if (node == NULL) {return NULL;}
    node->value = item;
    node->next=*head;  head is the current state pointer
    head=node;
    return head;
}

this should work

Python_user
  • 1,378
  • 3
  • 12
  • 25
rawnish
  • 11
  • 1
  • 4
  • It wasn't my down-vote, but…you managed to introduce `head` which was not mentioned in the question. You did set `node->next` to a value, which is a very good idea; it is just the choice of value that is debatable. And, if you need to use `node->next = *head;` then you need to write `*head = node;` and return `*head` (or `node`). – Jonathan Leffler Jan 25 '15 at 08:12