0

I wrote some code that uses function pointers to link modules functions in c. I was wondering is it possible to use those function pointers to access state in another translation unit.

I wrote this struct to handle life cycle functions so that I can have a module doing everything, you pass in a pointer to this struct with your functions and they get called when they are suppose to.

typedef struct {
    void (*init_func_ptr)(void);
    void (*fixed_update_func_ptr)(void);
    void (*variable_update_func_ptr)(double alpha);
    void (*variable_render_func_ptr)(double alpha);
    void (*pause_func_ptr)(void);
    void (*resume_func_ptr)(void);
    void (*resize_func_ptr)(int width, int height);
    void (*handle_event_func_ptr)(key_event *e);
    void (*quit_func_ptr)(void);
} lifecycle_ptrs;

i typically set it up like this

    lifecycle_ptrs* ptrs;

    ptrs = calloc(1, sizeof(lifecycle_ptrs));
    ptrs->init_func_ptr = &init;
    ptrs->fixed_update_func_ptr = &fixed_update;
    ptrs->variable_update_func_ptr = &variable_update;
    ptrs->variable_render_func_ptr = &variable_render;
    ptrs->pause_func_ptr = &pause;
    ptrs->resume_func_ptr = &resume;
    ptrs->resize_func_ptr = &resize;
    ptrs->handle_event_func_ptr = &handle_events;
    ptrs->quit_func_ptr = &quit;

this ptrs struct is setup in the module that defines main and is passed as a function argument. Once this is passed the program goes into an infinite loop until the user requests to quit.

So far this works pretty well and does what I want to do.

What I was curious about now is, is it possible to have something like this where I am able to access variables from the other module?

Let's say module a looks like this

modulea.c
int a;
int b;
char t[] = "test";

moudleb.c
struct t;
float 12.0;

is it possible to access module a's member variables from moduleb or visa versa?

I could write functions to get each thing but I feel there might be more elegant and maintainable way to do that. I could also define a struct and do a swap function where module a get access to the variables in module b and visa versa but again this seems a bit odd to me.

Are there any other ways to do this? I am more concerned about maintainability but any insights would help because right now I am at a loss.

user1610950
  • 1,837
  • 5
  • 33
  • 49
  • Possible duplicate of [How do I use extern to share variables between source files in C?](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c) – Roman Zaitsev Feb 02 '16 at 08:40

2 Answers2

4

Yes, technically it is possible, simply use extern.

However, there is no reason why you should ever do that. If you ever come up with a need to access (non-constant) variables in another file, then that means that your program design is broken.

And if you go through with extern, it will become even more broken, you'll get complete spaghetti code with very tight coupling, where everything in the program depends on everything else and all bugs will fail-escalate through the program and tear down things completely unrelated to the bug.

So the proper solution is to step back and think twice about the overall program design.

Instead of such spaghetti-coding, use object-oriented design and write each h and c file pair so that they together form an autonomous module (call it class, ADT or what you will), which is only concerned with it's own designated purpose. All file scope variables inside such a c file should be declared static to block access from outside. Access to such variables, if at all needed, should be done through setter/getter functions. The only kind of variables that is acceptable to share directly across files are read-only constants.

Though note that you should avoid file scope variables as far as possible, as they will make the code non-reentrant.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • thanks, I don't think that I'll be going the extern route as I would prefer to keep the global namespace clean. I already use static for my variables in each translation unit. The main purpose that I want to access external variables is because the design is a bit fluid right now. Also I avoid most OOP, that's why I am only using C and not C++ – user1610950 Feb 02 '16 at 12:57
  • @user1610950 OOP is universally regarded as good design, and since it is about program design, it is independent of programming language used. Most importantly, you should be creating autonomous code modules with no dependencies and private encapsulation - this is a must. Other things like inheritance and code re-usability can be nice but are not nearly as important. Similarly, there is nothing in C++ that makes your code get an object-oriented design automatically, it just has some handy features that makes OOP a bit prettier, like constructors/destructors. – Lundin Feb 02 '16 at 13:04
0

Easiest way to share members between different modules is to use headers files.

You could add a modulea.h file to your project like:

#ifndef modulea_H_
#define modulea_H_

extern int a;
extern int b;
extern char t[];

#endif

Then you can #include "modulea.h" in your moduleb.c source file.

LPs
  • 16,045
  • 8
  • 30
  • 61