Thread: trouble to understnad function in code

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

    trouble to understnad function in code

    Hello,
    I am trying to understand this program.
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    struct node 
    {
        int data;
        struct node *next;
    };
    
    
    struct node* AddToFront (struct node *head, int value)
    {
        struct node *new = malloc(sizeof(*new));
        
        if ( new != NULL)
        {
            new -> data = value ;
            new -> next = head;
        }
        
        return new;
    }
    
    
    void show (struct node *head)
    {
        struct node *current;
        
        current = head;
        
        while (current != NULL)
        {
            printf("%d\n", current -> data); 
            current = current -> next;
        }
    }
    
    
    
    
    int main ()
    {
        struct node *head = NULL;
        head = AddToFront( head, 1 );
        head = AddToFront( head, 2 );
        head = AddToFront( head, 3 );
        show (head);
        return 0;
    }
    I don't understand how does function show work in program

    function takes value of head pointer
    current is structured pointer which is declered inside function
    value of head pointer being assigned to current structure pointer
    after that it's being checked that current is not null

    Code:
     void show (struct node *head){
        struct node *current;
        
        current = head;
        
        while (current != NULL)
        {
            printf("%d\n", current -> data); 
            current = current -> next;
        }
    }
    I don't understand two statement inside while loop

    I don't understand what does it mean current -> data

    I don't understand what does it mean current = current -> next;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,156
    Not much has happened in the two years since you asked basically the same question.
    Printing address of structure pointer
    pointer to structure in c

    You're not learning anything, it's just a catalog of random questions over the years.
    trouble to understnad function in code-screenshot-2022-02-22-04-29-34-jpg
    If you're actually serious about this, you need to do more than dabble.

    > I don't understand what does it mean current -> data
    > I don't understand what does it mean current = current -> next;
    But you knew what they meant in AddToFront surely.
    Perhaps you could re-read the thread where you copied this code from.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having some trouble with this C code
    By jayygerman in forum C Programming
    Replies: 17
    Last Post: 08-22-2013, 06:42 PM
  2. Trouble understanding some code please help
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 09:01 PM
  3. Code trouble
    By frterwil in forum C Programming
    Replies: 4
    Last Post: 02-11-2009, 11:32 AM
  4. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread