Thread: ...if I only Node...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas

    ...if I only Node...

    typedef struct Supreme
    Supreme *head;
    Supreme *prev;
    char *stringData;
    Supreme *next;
    Supreme *tail;

    CreateAddSupremeNode (Supreme *head)
    Supreme s = (Supreme *)malloc (sizeof(Supreme));
    if(head == NULL)
    { head = s;
    return s; }
    Supreme x;
    for(x = head; x != NULL; x = x->next)

    x->next = s;
    return s;

    ???This Crashes My System....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Supreme *CreateAddSupremeNode( Supreme **head )
        Supreme s = malloc( sizeof( Supreme ) );
        if (*head == NULL)
            *head = s;
            Supreme x;
            for( x=*head; x && x->next; x = x->next );
            x->next = s;
        return s;
    In both cases you were trying to use null pointers. If head was null, you cannot assign it the value of s. This:

    if( node->next == NULL ) node->next = s;

    Is not the same as:

    if( head == NULL ) head = s;

    The second problem was that your loop would make sure that 'x' was null, then you'd try assigning 'x->next' a value, and since 'x' was already null, BadThings(TM) happened.

    Hope is the first step on the road to disappointment.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Ok but why **head?

    Won't *head work?
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Consider a call to CreateAddSupremeNode...
    int main ()
     Supreme * head = NULL; // The list is empty.
     CreateAddSupremeNode (head); // This is bad...
     // Now the list is not empty, so the value of head is not NULL.
     //  BUT that means changing the value of a passed variable,
     //  and the only way to do that is to pass a reference to the
     //  variable, which would be...
     CreateAddSupremeNode (&head); // This is correct way to
     // change value of head.
     return 0;
    So, since head is a Supreme *, &head is gonna be a Supreme **, and instead of changing the value of head in your function, you'll have to change the value of *head.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Thanks, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
Website Security Test