Theory: getter / setter function or global variable? WITH

I am currently writing C LIBRABY where I need to use static void *.

In my first version, I put all my functions that need access to my void * in the same C file in which it was declared, for example:

main.c

int main()
{
  function_using_list();
}

foo.c:

void *list=NULL;

void *function_using_list()
{
  //stuff with list
}

In my second version, I made a small get / set function, so I can access my list through it from anywhere in my code:

foo.c

void   *getset_list(void nlist, int action)
{
   static void  *list = NULL;

   if (action == 1)
     return (list)
   else
     list = nlist;
}

What are the pros and cons of these two different projects? Is the second safe?

PS: I just realized that I forgot something in my question: this code is intended to be used as a library.

+3
source share
1 answer

, , - .

, , , , , , , .

- : getter setter. action=1 action=0:

static void *list = NULL;
void* get_list() {
    return list;
}
void set_list(void* newList) {
    list = newList;
}

"". , , static , , , .

+4

All Articles