Question: How should I organize implementations and declarations in C?


How should I organize implementations and declarations in C?

Answers 0
Added at 2016-12-10 16:12


I am trying writing my first little project and I don't know the best way to organize all of my head files and source files.

Is every head file has its corresponding source file (one on one), or there are couple source files that implemented one specific head file(several-for-one). What kind of function should be writing into a head files?

Should I jam all data structures(linked list, queue, stack, graph...) into a head file, however, it make the code looks unstraightforward. Or in another way, place them into different files according to its function(e.g if a queue is used to collect input strings, write it info "input.h").


I do not know how to write data structure in c in a brief and effective way.

As we all know, C doesn't support generic type, so it seems inevitable to write a several of same ADT that contain different ItemType.

For instance, I want to write a simple calculator, then I need to parse the expression, and It is necessary to use a double stack to collect numbers and a char* stack to collect operation symbols. I don't have a idea avoid writing same pop(), push(), size() and name them as double_pop(), string_pop(), double_push(), string_push(). This way multiply the number of functions, duplicate the codes, I think. If I am going to write a hashmap, the problem goes worse, because there are two arguments, and I have to name the functions like double_string_hashmap_init(double_string_hashmap *new_hashmap), double_string_hashmap_add(double a, String b), whose name it's super long and ugly.

Source Show
◀ Wstecz