...if I only Node...

This is a discussion on ...if I only Node... within the C Programming forums, part of the General Programming Boards category; [code] typedef struct Supreme Supreme *head; Supreme *prev; char *stringData; Supreme *next; Supreme *tail; CreateAddSupremeNode (Supreme *head) Supreme s = ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699

    ...if I only Node...

    [code]
    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
    Posts
    14,826
    Code:
    Supreme *CreateAddSupremeNode( Supreme **head )
    {
        Supreme s = malloc( sizeof( Supreme ) );
    
        if (*head == NULL)
        {
            *head = s;
        }
        else
        {
            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.

    Quzah.
    Hope is the first step on the road to disappointment.

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

    Won't *head work?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Consider a call to CreateAddSupremeNode...
    Code:
    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
    Unregistered
    Guest
    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 Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21