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;
int count; //Id
char name; //Name
char time1; //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.
Static variables are only visible within the C file they are defined. My guess is your typedef / struct / and definition of directorio is in a .h file. When you include this .h file in a .c file, it will create a local static variable for each .c file. These are 2 completely separate instances of the variable, yet both have exactly the same name. What you probably want is a single directorio variable and have all modules share this single variable.
Thank you Robert,
You are absolutely right in your comment, however all the creation of the list (structure) and its use are within the same main.c file.
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.
directorio = crearUserdir(directorio,"String1","String2");
This is the code of funtion:
dir_users * crearUserdir(dir_users *drusr, char name1,char time2)
dir_users *newnode= (dir_users*) malloc(sizeof(dir_users));
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;
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.
View all questions in Keil forum