Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C generic programming

First of all, I am a beginner programmer (still much to learn). In one of my small school projects I have written a stack for a struct . Now I have a slightly different struct and I need a stack for this one too. Should I write another data structure [stack] (very similar to the initial one), or try to achieve some generic programming...

Do you know any good generic programming strategies (online resources eventually) in C ? I've used google, but I haven't found nothing satisfactory, as most of the results are about C++ strategies.

Thanks!

LATER EDIT: After some reading, and experiencing, eventually I've found two solutions for my problem. I've documented them here: http://andreinc.net/2010/09/30/generic-data-structures-in-c/ . That article may contain errors or inexactitudes, still it summarize what I know so far.

like image 875
Andrei Ciobanu Avatar asked Dec 26 '09 11:12

Andrei Ciobanu


People also ask

Are there generic types in C?

Unlike C++ and Java, C doesn't support generics. How to create a linked list in C that can be used for any data type? In C, we can use a void pointer and a function pointer to implement the same functionality. The great thing about void pointer is it can be used to point to any data type.

Which is a generic programming?

Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.

What is generic programing in C++?

Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations. In C++, class and function templates are particularly effective mechanisms for generic programming because they make the generalization possible without sacrificing efficiency.

Can you template in C?

C does not have static templates, but you can use macros to emulate them.


2 Answers

Finding commonalities and creating abstractions is one of the most valuable skills for a programmer. As you're still learning, I'd suggest you do the following things:

(1) Implement the stack for that other struct. Yes, it's double work, but at your stage every working program counts. Builds up experience.

(2) Compare the programs. What are the parts they have in common? What are the parts that differ? Your goal is to separate the parts that are common from the parts that are different. What are the means that these two groups use to communicate? The parts that they have in common go into one part of your system (stack.h/stack.c), the parts that are different go into their own files (account.h/c, person.h/c, etc.). And the part where you combine them should do an include of stack.h and the parameterizing entity.

(3) Try to find all possible ways you know that the language offers that you can use to implement the abstract struct functionality. At first it always seems as if there is only one way, but for every non-trivial problem there are alsway several approaches. In the stack case, using standard C, for example, zou can use void pointers, you can use preprocessor macros, you should look into token pasting, you can use function pointers plus struct pointers, etc.

(4) Implement as many of them as possible. Again, this is for the learning experience. C has so many traps, and the earlier you run into them, the better.

(5) After you have enumerated and implemented all these different approaches, you should evaluate them: Which one was easiest to use? Which one was easiest to implement? Which one is fastest? Which one is easiest to debug?

like image 58
Carsten Kuckuk Avatar answered Sep 16 '22 13:09

Carsten Kuckuk


I don't do a lot of C hacking, but I think the way to go with this is void*.

So, just rewrite your stack of push/pop void* instead of some_struct*. It becomes your problem to keep the types correct, but that's just a price you pay for using such a low-level* programming language.

*Not to imply that this is a bad thing.

like image 20
Kevin Montrose Avatar answered Sep 16 '22 13:09

Kevin Montrose