4

Let's say I have a two files : stack.h and queue.h. Both want to implement the function add(). If I include stack.h and queue.h in the same main.c for example, there will be a collision.

What is the recommended way to implement the add() function in both files?

Gradient
  • 2,253
  • 6
  • 25
  • 36
  • 1
    As the other answers said, and as in any programming language, all libraries you include must have a sane design with dedicated names which you accept. Otherwise, you will always have collisions in any programming language. Eg, if you include a header "terribad.h" which happens to reserve all variables on the planet, you might want to reconsider having "terribad.h" inside your project. – Dmytro Feb 28 '13 at 00:09

2 Answers2

7

If this is pure C, the simplest common solution is to use do-it-yourself namespacing on the functions relative to the name of the module or data structure that they apply to. e.g.

stack_add(...)
queue_add(...)

You will find examples of this in pretty much all large pure-C projects:

pthread_mutex_lock() // POSIX/UNIX, used for locking a mutex
CGRectMake()         // iOS, used to fill in a CGRect structure
RtlZeroMemory()      // WinNT, "Rtl" indicates the module it belongs to

Your question was about "exported" (public) function names, but note too that when your .c files have private helper functions, you can mark them static which tells the compiler that they are used only from within that file, so your naming can be a little looser for them knowing they won't collide elsewhere.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • 1
    See this question for ideas on C namespaces, although IMHO it's a bit overkill: http://stackoverflow.com/questions/389827/namespaces-in-c – congusbongus Feb 28 '13 at 00:10
7

If you already have a queue and a stack implementation tested, and referred to elsewhere.. you can do some preprocessor tricks like this:

#define _STACK
#define _QUEUE

#ifdef _QUEUE
#define add queue_add
  #include "queue.h"
#undef add
#endif

#ifdef _STACK
#define add stack_add
  #include "stack.h"
#undef add
#endif

int main()
{
   stack_add();
   queue_add();
}

My advice is - refactor the code base to use non-clashing naming convention instead of generic function names like add and subtract and the rest.

If you are a fan of object oriented programming style and like the whole "add", "modify" concept then use function pointers inside the queue and stack structure.

#define TYPE int

typedef struct tagStruct{
   TYPE *dataBuffer;
   void (*add)(TYPE data);
   void (*modify)(TYPE data, int index);
   void (*deleteEverything)()
}stack;

and in the init(stack) method, assign add(), modify() and deleteEverything() to different functions that have no name-collision(likewise for queue)

Then you begin to see stack and queue as ENTITIES rather than bunch-of-functions.

You can then do this:

stack stc;
init(stc); /*sets up the function pointers*/
stc.add(10);
stc.modify(30,0);
stc.deleteEverything(); 
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 2
    @KeithNicholas yeah - but if he already has stack and queue defined, he will have to do something ugly. Just so he does not break other parts of the code that either use `stack.h` or `queue.h` – Aniket Inge Feb 28 '13 at 00:12