Thread: Pointer Riddle that is bugging me

  1. #1
    Unregistered
    Guest

    Angry Pointer Riddle that is bugging me

    Here is a snippet of code that is bugging me. The riddle is to allocate a node for a linked list in a function without returning the return statement.

    So far I have this

    #include <stdio.h>
    #include <stdlib.h>


    struct node
    {
    int value;
    struct node *next;
    } ;

    void NewNode(struct node *newnode)
    {
    struct node *newnodeinstance;
    newnodeinstance = malloc(sizeof(node));
    newnodeinstance->value = 0;
    newnodeinstance->next=NULL;
    *newnode = newnodeinstance;
    }


    void main()
    {
    struct node *head;
    int i=0;
    NewNode(head);
    head->value=100; // This causes unhandled exception. basically head is still NULL when I return from function.
    }


    What am I missing !!

    -sl

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You know, I don't think that you actually allocate memory for head at any point.

    I'm not criticizing you, I'm just going to point out that this is a firly obtuse way of doing this, since returning a pointer to the node would be the technically right way. (Yea yea, I know, it's a riddle).

    Is this what you're looking for?
    Code:
    void NewNode(struct node ** newnode) 
    { 
    * newnode = malloc(sizeof(node)); 
    (* newnodeinstance)->value = 0; 
    (* newnodeinstance)->next=NULL; 
    } 
    
    
    void main() 
    { 
    struct node *head; // No reason to believe head != NULL...
    int i=0; 
    NewNode(&head); // We can change head to a usefull value
      // by passing it's address.
    head->value=100; // Dunno if it'll work.
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just noticed a problem too...
    Code:
    *newnode = newnodeinstance;
    Is using incompatible types (assigning a noode * to a node), which I'm surprised the compiler didn't pick up. Try
    Code:
    *newnode = *newnodeinstance;
    Although quite frankly, I'd still expect your program to crash with that code... just maybe it'll crash earlier.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    The only change you need to make is the following:

    Don't bother making a new pointer variable, as it's not useful for anything. Change:

    struct node *newnodeinstance;
    newnodeinstance = malloc(sizeof(node));
    newnodeinstance->value = 0;
    newnodeinstance->next=NULL;
    *newnode = newnodeinstance;

    to:

    newnode = malloc(sizeof(node));
    newnode->value = 0;
    newnode->next=NULL;

    If you MUST use your original way, the only error is that:
    *newnode = newnodeinstance;
    is incorrect, use:
    newnode = newnodeinstance;

    But, it's probably best to forget about newnodeinstance altogether.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM