This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

static structure in C keep list of information in memory

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,

Parents
  • 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.

Reply
  • 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.

Children