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,
Sorry. I saw the Zebra.
You have a pointer that should point to a list of items that are linked together as they are created. It starts out as NULL. When you create the first item, directio should point to this item that you created space for somehow. If you are properly creating space for the item in the CrearUserdir, you will need to pass pointer to the pointer to the head of the list so that you may actually change it from null to the actual first item of the list.
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.