ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was thinking about how to make a dynamic array in C, and I thought that it would be much better if it could accept any data type. Is this possible? Of course I thought about void pointers, but how do I store the actual data?
I was thinking about how to make a dynamic array in C, and I thought that it would be much better if it could accept any data type. Is this possible? Of course I thought about void pointers, but how do I store the actual data?
Use smart enough preprocessor - if you want compile time type checking.
I really don't understand your perverse fascination with taking C++ constructs and trying to implement them verbatim in C. In previous posts, it was C++ style inheritance. In this post, C++ style templates. I mean, you certainly *can*. In the same sense you can certainly eat stringbeans through your nose (if you try hard enough). But *why*????
Anyway: let's say you want a "generic linked list" (for example) in C. You want the *same* code to be able to handle *different* types, but you *also* want to "remember" the type as you store it in the list.
Easy. Many alternatives.
But arguably the simplest, most straightforward approach would be:
1. Have your linked list store pointers (duh!)
2. Always store a pointer to a "struct" (never a "bare primitive" like char or double)
3. Your struct contains a "tag" identifying the "type"
4. The same .h header that defines your struct also defines your "tags" (e.g. an enum of "known types").
IMHO .. PSM
PS:
alunduil and I posted almost exactly the same time. A union is of structs would also work, but I think you'd also probably need the tag, to retain knowledge of the original "type".
If you choose this approach, you might want to write a C pre-preprocessor.
Further developing the approach - such generic things depend in reality not on type, but rather on sizeof(type). So a smarter preprocessor can really help - it will create new functions not for every new type, but for every new sizeof(type).
In a code of mine I am importing into Perl "C" numeric macros by creating a small test program which printf's the macro to be imported in the format of my choosing. I.e. the smart preprocessor can be made "C" types aware in such a manner.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.