0

I try to write a code to read few times a linked list without call a specific function like void print(t_list *list), see the code below. I can figure why my code cannot be read a second time my linked list in the while context, because at the end of my first loop my struct t_list is NULL, and when I start the second while my struct is still NULL and obviously nothing can be used.

So I have two questions,

first : Why I can read twice my linked list when I pass by the function void print(t_list *list), it's good because that's work but I don't understand why in the second read my t_listis not NULL

second : How in the whileor for context, rewind my pointer t_list to the begin, to read again the linked list.

#include <stdlib.h>
#include <stdio.h>

typedef struct s_list t_list;
struct s_list {
    int arg;
    t_list *next;
};


int add(t_list **ref, int arg) {
    t_list *temp;
    temp = NULL;
    if(!(temp = (t_list*)malloc(sizeof(t_list))))
        return (0);
    temp->arg = arg;
    temp->next = (*ref);
    (*ref) = temp;
    return(1);
}

void change(t_list *list) {
    printf("change arg:\n");
    while(list) {
        list->arg = 42;
        list = list->next;
    }
}

void print(t_list *list) {
    printf("list:\n");
    while(list) {
        printf("arg: %i\n",list->arg);
        list = list->next;
    }
}

int main() {
    t_list *list;
    list = NULL;
    add(&list, 0);
    add(&list, 1);
    add(&list, 2);
    print(list); // work fine
    change(list);
    print(list); // work fine it's possible te read a second time but why ?
  
    // that's way don't work a second time
    while(list) {
        printf("arg: %i\n",list->arg);
        list = list->next;
    }

    while(list) {
        printf("arg: %i\n",list->arg);
        list = list->next;
    }

    return (0);
}

console

➜  LINKED_LIST git:(master) ✗ clang linked_list.c && ./a.out
print list:
arg: 2
arg: 1
arg: 0
change arg:
print list:
arg: 42
arg: 42
arg: 42
while arg: 42
while arg: 42
while arg: 42
Knupel
  • 323
  • 2
  • 14
  • The list inside print is a local variable. It doesn't change the one in main. – stark Oct 14 '20 at 10:50
  • @stark why it's a local varial,in the `void print(t_list *list)` the `*` don't indicate it's a pointer ? – Knupel Oct 14 '20 at 11:03
  • pointer variables can be local too. – Rohan Kumar Oct 14 '20 at 11:04
  • 1
    function arguments are copies. You can change the name in print to plist to make it clear. – stark Oct 14 '20 at 11:07
  • _"second : How in the while or for context, rewind my pointer t_list to the begin[?]"_. Linked lists are not _re-wound_ per se, but you can search for a list by searching the values of its members, i.e. `while(list->arg != 1){//go to next node}. – ryyker Oct 14 '20 at 12:19
  • If you passed the value of an integer variable to a function, and that function changed the value of its parameter, would you expect the value of the caller's variable to change as well? (Your answer should be "No".) Well, it's the same for pointer variables. – Ian Abbott Oct 14 '20 at 12:22

1 Answers1

1

As pointed out in comments, I think you should read about C Variable Scopes.

When you pass list into print() method value of pointer variable t_list* list gets copied to the argument of print() method. So whatever you do print method's t_list* list doesn't affect main method's t_list* list. You can confirm this by checking value of list before and after calling print() method:

    printf("%p\n", list);
    print(list); 
    printf("%p\n", list);
    print(list); 
    printf("%p\n", list);

They all should print same value since list hasn't been changed in main function's scope:

0x17222e0
list:
arg: 2
arg: 1
arg: 0
0x17222e0
list:
arg: 2
arg: 1
arg: 0
0x17222e0
arg: 2
arg: 1
arg: 0
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
  • Are you sure is local variable, be cause when I write a function to change the value, the changement is effective. I update the question to show that. – Knupel Oct 14 '20 at 11:19
  • That local variable is storing address of your linked list head node. So when you say you make changes, you're making changes to the variable to which `list` pointer is pointing. – Rohan Kumar Oct 14 '20 at 11:22
  • 1
    Bdw, you're passing `&list` in add methods but plain `list` in print method. Both of them are different things. Former is pointer to a pointer and latter is pointer to a variable – Rohan Kumar Oct 14 '20 at 11:28