Thread: why is the linked list not being loaded ?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10

    why is the linked list not being loaded ?

    So I was trying my hands on a linked list in c, although I think I have done all well, my list is not being loaded when I try to load it...can somone point me to where I'm going wrong? now I post the snippet.. when I try to print out the items in the list nothing is printed out and when I try to count the items I get a count of zero...so I'm surely doing something wrong ...
    Code:
    typedef struct n
    {
      char name [100];
      //double price;
      struct  n * next;
    } node;
    void loadItemsIntoList(node * testa, char nome [] );
    
    
    int main()
    {
    node * list = 0;
    
    
    //random list loading..
     loadItemsIntoList(list, "fff");
     loadItemsIntoList(list, "ssss");
     loadItemsIntoList(list, "sdfsd");
     loadItemsIntoList(list, "lknou");
    
    
     for (node * curr = list; curr != 0; curr = curr->next)
     {
         printf("%s", curr->name);
     }
     //testing for length of list
     int count = 0;
     for(node * curr = list; curr != 0; curr = curr->next)
     {
       count += 1;
     }
    printf("\nlength of node is %d ,", count );
    return 0;
    }
    
    
    void loadItemsIntoList(node * testa, char nome [] /*, double prezzo */)
    {
         node * temp = malloc(sizeof(node));
         strcpy(temp->name, nome);
         //temp->price = prezzo;
         temp->next = 0;
      if(testa == 0)  {  testa = temp; }
      else{
            node * curr = testa;
            while(curr->next != 0){curr = curr->next;}
            curr->next = temp;
         }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    loadItemsIntoList modifies testa, but that change isn't returned back to main.

    You need to do something like
    list = loadItemsIntoList(list, "fff");
    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
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    Quote Originally Posted by Salem View Post
    loadItemsIntoList modifies testa, but that change isn't returned back to main.

    You need to do something like
    list = loadItemsIntoList(list, "fff");
    please what do you mean by "that change isn't returned to main? it doesn't say anything and with "you need to do something like this" well what prevents me from doing like how I did...loadItemsIntoList(...) returns a void ...
    can you please be clearer?

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    All this boils down to the following example:
    Code:
    #include <stdio.h>
    
    void foo(int i) {
        i = 42;
    }
    
    int main() {
        int data = 0;
        foo(data);
        printf("%d", data);
    }
    What should be the printed value?

    More specifically to your code - in C there's only pass-by-value when it comes to function parameters. The so called "pass by reference" (pass by pointer) in C is a convention of making the function take a pointer as a parameter, but the value of a pointer - the address - is still passed by value. It's copied.

    Note the last sentence - it's copied. So if you work on a local copy - the change isn't reflected at the original.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    Quote Originally Posted by Xupicor View Post
    All this boils down to the following example:
    Code:
    #include <stdio.h>
    
    void foo(int i) {
        i = 42;
    }
    
    int main() {
        int data = 0;
        foo(data);
        printf("%d", data);
    }
    What should be the printed value?

    More specifically to your code - in C there's only pass-by-value when it comes to function parameters. The so called "pass by reference" (pass by pointer) in C is a convention of making the function take a pointer as a parameter, but the value of a pointer - the address - is still passed by value. It's copied.

    Note the last sentence - it's copied. So if you work on a local copy - the change isn't reflected at the original.
    a bit strange to me coming from c++, can you tweak my code a bit to make it work? I mean to make the list load the values? it will be a good and better example to the theory you just gave, I will see the difference..
    infact in the short example you gave, I responded of course foo should update data to 42...but I just compiled and run it and no..it was still printing 0....I'm shocked

  6. #6
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    just did more test and the same thing is happening in c++, I got it that is boils down to locality, in the case of foo after foo exist it cleans up everything so data remains the same in main, I WAS THINKING data should be able to be updated by calling foo...but may be not because of function space and every function'ss activation record...
    now I have to do some more test, but one should be able to modify a pointer having it address irrespective of where it is situated respective of a function..or I'm I wrong..?

    The so called "pass by reference" (pass by pointer) in C is a convention of making the function take a pointer as a parameter, but the value of a pointer - the address - is still passed by value. It's copied.
    if the address is copied..then I should be able to modify the value the address is pointing to right? because the value the address is pointing to is always one...
    now lets see how this new realisation can be applied to the work at hand...

  7. #7
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    so in C can't I do something like
    voidloadItemsIntoList(node * &testa, charnome [] ) ; and pass the pointer by reference?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can pass a pointer to a pointer.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    ok, can you write a short snippet? taking into consideration the linked code that I posted up there?

  10. #10
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Yes, when you pass the address by copy you can use that address to access a value under it regardless of it (the address) being a copy.
    Now, to take the sample test case I posted before - how do you modify it taking into account that C doesn't have actual reference type like C++? Well, you can pass in an address of the value instead of the actual value. That way you can modify the value from within the called function, even though the value itself is "outside", as in, doesn't live in the function local scope.
    Code:
    void inc(int* ptr) {
        ++(*ptr); //we dereference the pointer to get to the value, and then, increment the value
    }
    //...
    int d;
    inc(&d); //& here is address-of operator
    Does this make sense to you? Also, consider what would happen if you called it like so:
    Code:
    inc(NULL);
    //or
    int* ptr = function_that_can_return_null_pointer();
    inc(ptr);

    So if what you want is to modify actual value-of-the-pointer (as opposed to the value the pointer is pointing at) - what would you do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread