Llist questions

This is a discussion on Llist questions within the C Programming forums, part of the General Programming Boards category; I have a few questions... Code: #include <stdio.h> #include <stdlib.h> struct node { int x; struct node *next; }; int ...

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    Llist questions

    I have a few questions...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
    int x;
    struct node *next;
    };
    
    int main(void ) {
    struct node *root;
    struct node *conductor;
    
    root = malloc( sizeof(struct node) ); /*which struct does this point to? The actual global struct or the struct inside of the global struct?*/
    root->next = NULL;
    root->x=10;
    
    conductor = root;
    
    /I still don't get the whole if conductor or if conductor->next does not equal null concept because above I have set root->next to equal exactly that, NULL.*/
    if(conductor != NULL) {..
    while(conductor->next != NULL) {..
    conductor = conductor->next;..
    .... ..}
    }
    
    conductor->next = malloc( sizeof(struct node) );
    conductor = conductor->next;
    
    if(conductor = NULL)..
    fprintf(stderr, "Out of memory.\n");
    
    else{
    conductor->next = NULL;
    conductor->x = 20;
    }
    
    
    }
    Thanks in advanced...

  2. #2
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    On a side note sorry for the weird or random punctuations I wrote that on my iPhone. And it sometimes adds stuff on it's own

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Annonymous View Post
    Code:
    root = malloc( sizeof(struct node) );
    /*which struct does this point to? The actual global struct or the struct inside of the global struct?*/
    It points to the new struct you just allocated memory for with malloc, since malloc returns a pointer to the memory you requested.


    Quote Originally Posted by Annonymous View Post
    /I still don't get the whole if conductor or if conductor->next does not equal null concept because above I have set root->next to equal exactly that, NULL.*/
    Code:
    if(conductor != NULL) {..
    while(conductor->next != NULL) {..
    conductor = conductor->next;..
    .... ..}
    }
    In this case it does not matter since you know that conductor->next is NULL, but if this was a separate function not main, and you handed that function a list with thousands of nodes in it the while loop would make sure that conductor->next pointed to the end of the list after the loop and your insert point for the new node.

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    @subsonics, Sure malloc returns a pointer to the memory I requested but which struct node am I using with the sizeof function? ..
    Code:
    //..root = malloc ( sizeof(---> struct node <---) );..
    .... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ...... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
    struct node { /*This struct node*/
    .... ..int x;..
    .... ..struct node *next; /*or this struct node?*/
    .... ..};

    And ok I understand the conductor->next thing now. It does equal NULL but the code at the end ..is just for that purpose. It adds a new node if it reaches the dummy node, and in this case it did. Am I wrong?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Annonymous
    which struct node am I using with the sizeof function?
    You're using the type name, not an object. Furthermore, the expression that is the operand of sizeof is not actually evaluated, so you could write:
    Code:
    root = malloc(sizeof(*root));
    even if root was a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The size comes from the definition of the struct...

    When you get to the end of the list your pointer to the next struct should be NULL, when you allocate memory for your new struct you assign it to that pointer and set the new pointer to NULL, creating a new end of list.

  7. #7
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    @laserlight, why is that? The operand of sizeof, not evaluated.

  8. #8
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Is the answer the first sentence of commontater's response? Because it uses the overall size of the struct?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Annonymous
    why is that? The operand of sizeof, not evaluated.
    Because the rules of the C programming language say so. (Variable length arrays are an exception.)

    Quote Originally Posted by Annonymous
    Is the answer the first sentence of commontater's response? Because it uses the overall size of the struct?
    No, that has to do with the result of sizeof, not with the operand of sizeof.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Ok thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Gui questions
    By h3ro in forum Windows Programming
    Replies: 2
    Last Post: 08-05-2008, 02:11 AM
  3. questions about if, or, not, and etc
    By sway1 in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2008, 03:32 PM
  4. New to C#, have some questions
    By dudeomanodude in forum C# Programming
    Replies: 3
    Last Post: 04-28-2008, 03:29 AM
  5. Having trouble with my llist.
    By Qui in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2004, 10:23 AM

Tags for this Thread


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