Thread: Why print statement doesn't show structure value

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    72

    Why print statement doesn't show structure value

    This is my code

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    struct node 
    {
        int data;
        struct node * next;
    };
    
    
    void ADD ( struct node *Head, int value)
    {
        struct node * new = malloc( sizeof(*new));
        if ( new != NULL )
        {
            new->data = value;
            new->next = NULL;
        }
        
        if ( Head == NULL )
        {
            Head = new;
        }
    }
    
    
    int main ()
    {
        struct node * head = NULL;    
        ADD(head,2);
        printf("%d", head-> data  );
        
        return 0;
    }
    I am expecting this line should print 2 but I don't get any value
    Code:
    printf("%d", head-> data  );

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Because you don't return the pointer from the function.

    Do you understand why this doesn't print 42?
    Code:
    void change(int foo) {
      foo = 42;
    }
    int main ( ) {
      int x = 0;
      change(x);
      printf("Result=%d\n", x);
    }

    You need to start paying attention to your previous threads.
    There are about a dozen threads from you on just lists.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post

    Do you understand why this doesn't print 42?
    Code:
    void change(int foo) {
      foo = 42;
    }
    int main ( ) {
      int x = 0;
      change(x);
      printf("Result=%d\n", x);
    }
    Yes, because we are only passing copy of variable

    Quote Originally Posted by Salem View Post
    You need to start paying attention to your previous threads.
    There are about a dozen threads from you on just lists.
    I am having hard time to understand linked list. I am reading my old threads many time and trying to understand them

    Quote Originally Posted by Salem View Post
    Because you don't return the pointer from the function.
    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    struct node 
    {
        int data;
        struct node * next;
    };
    
    
    
    
    struct node * ADD ( struct node *Head, int value)
    {
        struct node * new = malloc( sizeof(*new));
        if ( new != NULL )
        {
            new->data = value;
            new->next = NULL;
        }
        
        if ( Head == NULL )
        {
            Head = new;
        }
    	return Head;
    }
    
    
    
    
    int main ()
    {
        struct node * head = NULL;    
        head = ADD(head,2);
        printf("%d", head-> data  );
        
        return 0;
    }
    Can it be written in another way that doesn't require returing pointer from function

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Quote Originally Posted by Dadu@
    Can it be written in another way that doesn't require returing pointer from function
    The key is to understand why do we return a pointer from the ADD function in the first place. The answer to that is that we want to update the head pointer from the caller, and we cannot do that within the function as we only have a copy of that head pointer. Hence, we return the updated pointer so that the caller can assign it to the head pointer from the caller, thereby updating it.

    So, what other ways can we use to update the head pointer from the caller? One common method is to pass a pointer to the head pointer, e.g.,
    Code:
    void ADD(struct node **Head, int value)
    {
        struct node *new = malloc(sizeof(*new));
        if (new != NULL)
        {
            new->data = value;
            new->next = NULL;
        }
    
        if (*Head == NULL)
        {
            *Head = new;
        }
    }
    Note that you didn't implement the code to handle what happens when the head pointer is not a null pointer. I would also keep things simple by handling malloc returning a null pointer by returning early, rather than having to think about whether it is safe for new to be a null pointer further down in the function. For example:
    Code:
    void ADD(struct node **Head, int value)
    {
        struct node *new = malloc(sizeof(*new));
        if (!new)
        {
            return;
        }
    
        // from here on you know that new is not a null pointer...
        new->data = value;
        new->next = NULL;
    
        if (*Head == NULL)
        {
            *Head = new;
        }
    }
    This also opens the possibility of returning an error code, whereas if you designed this function to return the head pointer, then you might need another pointer parameter to record an error code that the caller can examine.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string doesn't print in structure
    By vajra11 in forum C Programming
    Replies: 2
    Last Post: 10-28-2019, 12:10 PM
  2. Array doesn't show correct value
    By vajra11 in forum C Programming
    Replies: 7
    Last Post: 08-05-2018, 04:51 AM
  3. Replies: 18
    Last Post: 08-31-2012, 10:17 AM
  4. Replies: 11
    Last Post: 05-06-2007, 11:26 PM
  5. Program doesn't show up
    By Krasimir11 in forum C Programming
    Replies: 7
    Last Post: 01-05-2006, 06:21 PM

Tags for this Thread