Thread: Passing and Returning a struct

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    Passing and Returning a struct

    Here Linked list - Wikipedia is an example program. given description is little bit confusing
    Code:
    nodeaddNode(nodehead,intvalue){
       node temp, p; // declare two nodes temp and p
       temp = createNode(); // assume createNode creates a new node with data = 0 and next pointing to NULL.
       temp->data = value; // add element's value to data part of node
       if (head == NULL) {
           head = temp;     // when linked list is empty
       }
       else {
           p = head; // assign head to p 
           while (p->next != NULL) {
               p = p->next; // traverse the list until p is the last node. The last node always points to NULL.
           }
           p->next = temp; // Point the previous last node to the new node created.
       }
       return head; 
    }


    I think it say the passing and returning pointers to structs.

    this function taking value of pointer to structure and returning value of value of pointer to structure
    Code:
    struct node* addNode(node head)
    Am I correct ?
    Last edited by Player777; 01-15-2020 at 08:10 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The programming language that that code was written in was unstated. It certainly looks like C, and with the right declarations it will probably be compilable as C, but I suggest treating it as pseudocode: code for you to understand and implement in your programming language of choice, rather than being too caught up in the syntax.

    That said, if we regard it as C, then node is likely to be a typedef for a pointer to a node structure. This is bad practice as it obscures the fact that node is in fact a pointer rather than a non-pointer type, yet objects of the node type are expected to be used as pointers rather than as non-pointers or some opaque pointer/handle.

    So yes, you're right to say that addNode accepts a pointer to a struct type as its first argument and returns a pointer to a struct type, but no, you shouldn't follow that Wikipedia article by writing your own code with a non-opaque pointer typedef for node. Treat the Wikipedia code as pseudocode, not C.
    Last edited by laserlight; 01-16-2020 at 12:20 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by laserlight View Post
    Treat the Wikipedia code as pseudocode, not C.
    I have to write the function that add the new node after the last node created.

    The head pointer will point to the address of the new node and the pointer of the new node will point to the pointer of the last node.


    Code:
    #include<stdio.h>
     #include<stdlib.h>
      
    struct node
    {
        int n;
        struct node *next;
    };
       
    struct node* addnode( int data, struct node *head, struct  node*tail )
    {
      struct node *addnew = malloc(sizeof(*addnew));
      
      addnew = head = tail;
      tail->n = data;
      addnew->next = tail;
      
      return tail;
    }

    1 = head = addnew = tail
    1 2 = (head = 1, addnew =2, tail = null)
    1 2 3 = (head =2, addnew = 3, tail = null)
    1 2 3 4 = (head =3, addnew = 4, tail = null)
    Last edited by Player777; 01-16-2020 at 07:09 AM.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Not tested, but the below should work. Most of the comments are straight from the wikipedia pseudocode but I added a few. I suspect that Wikipedia uses the pseudo-c so that it's more language agnostic and avoids having to deal with pointers to pointers which is not helpful if you're trying to explain the logic only

    Code:
    struct node
    {
        int data;
        struct node *next;
    };
    
    // Modified from pseudo-c given at https://en.wikipedia.org/wiki/Linked_list
    // Need head to be a pointer to a pointer here because if the list is empty
    //  we need to be able to modify head to point to the first node in the list
    //  (which will be created in this function)
    struct node *addNode(struct node **head, int value)
    {
        struct node *newnode;
    
        // allocate memory for a new node. Return NULL if it fails
        newnode = malloc(sizeof (*newnode));
        if (!newnode)
            return NULL;
    
        // initialise the new node
        newnode->data = value; // add element's value to data part of node
        newnode->next = NULL;
    
        // append the node to the end of the list:
        if (*head == NULL) {    // when linked list is empty
            *head = newnode;    //    this is why we needed a pointer to a pointer
        }
        else {
            struct node *p;
            p = *head; // assign head to p (p points to the first node in the list)
            while (p->next != NULL) {
                p = p->next; // traverse the list until p is the last node. The last node always points to NULL.
            }
            p->next = newnode; // Point the previous last node to the new node created.
        }
        return *head;
    }
    Call the function using something like:

    Code:
    int main(void)
    {
        struct node *list = NULL;   // Empty list
    
        addNode(&list, 1);
    
        /* Create another function called, e.g. printList() */
        
        /* Create another function called, e.g. destoryList() and call it to free the allocated memory */
    
       return 0;
    }
    Edit: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf see pages 13 and 14 for why a pointer to a pointer is necessary (another option is to use a dummy node)
    Last edited by Hodor; 01-16-2020 at 08:03 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    Need head to be a pointer to a pointer here
    No, you don't. addNode returns a pointer to head, so you just need to use it:
    Code:
    list = addNode(list, 1);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    No, you don't. addNode returns a pointer to head, so you just need to use it:
    Code:
    list = addNode(list, 1);
    Ah, I see. I was kind of wondering why it was returning head. Thanks

    Edit: kind of annoying to have to remember to always have list = in the calling function though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Passing / Returning a Boolean (Help)
    By EDB in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2008, 02:56 PM
  3. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM
  4. returning struct and passing as an argument
    By ronin in forum C Programming
    Replies: 4
    Last Post: 06-07-2003, 11:21 AM
  5. Passing And Returning Values
    By neo6132 in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 10:24 PM

Tags for this Thread