Hello everyone,
I would like to know how I can create a static data type structure where the information is always kept in memory even if it calls different functions or executes some other code outside its main function.
Every time I call a function the data is lost on the list.
typedef struct director_users dir_users; struct director_users { int count; //Id char name[10]; //Name char time1[10]; //Pos struct director_users *p1; }; static dir_users *directorio=NULL;
When I create the linked list the information is stored correctly but when calling another function the information disappears from the list.
crearUserdir(directorio,"String1","String2"); functionTest();
Regards,
Exactly, pity the function source wasn't provided.
Or
directorio = crearUserdir(directorio,"String1","String2");
This is the code of funtion:
dir_users * crearUserdir(dir_users *drusr, char name1[10],char time2[10]) { dir_users *aux1=NULL; dir_users *newnode= (dir_users*) malloc(sizeof(dir_users)); if(newnode) { strcpy(newnode->name,name1); strcpy(newnode->time1,time2); newnode->p1=NULL; if(drusr==NULL) drusr=newnode; else { aux1=drusr; while(aux1->p1!=NULL) { aux1=aux1->p1; } aux1->p1=newnode; } } return drusr; }
Do you see that the item that directorio points to is not ever changed? If Drusr is null that means directories was null but you only change Drusr to not be null. You never modify the directories item. That is why you need to pass a pointer to the value held by directirio.
could be returning the value: else drusr=aux1;
else { aux1=drusr; while(aux1->p1!=NULL) { aux1=aux1->p1; } aux1->p1=newnode; drusr=aux1; }
Actually the original code does seems to return the head of the list all the time. It needs to be assigned back to the variable that contains the head of the list. Then this post would have never happened.
what you added seems to either return the head of the list (if it was NULL), or the item 1 before the last item in the (new) list.