Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:44:49+00:00 2026-06-05T14:44:49+00:00

I’ m trying to implement a queue structure using C. My implementation is very

  • 0

I’ m trying to implement a queue structure using C. My implementation is very simple; the queue can hold only ints and nothing else. I was wondering if I could simulate C++ templates in C(probably by using the preprocessor #define) so that my queue can hold any data type.

Note: I do not want to use void*. I think it is a bit risky and can easily cause bizarre runtime errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:44:51+00:00Added an answer on June 5, 2026 at 2:44 pm

    You can use subtle and ugly tricks in order to create that kind of templates. Here’s what I would do:

    Creation of a templated list

    Macro to define the list

    I would first create a macro – let’s call it say define_list(type) – that would create all the functions for a list of a given type. I would then create a global structure containing function pointers to all the list’s functions and then have a pointer to that global structure in each instance of the list (note how similar it is to a virtual method table). This kind of thing:

    #define define_list(type) \
    \
        struct _list_##type; \
        \
        typedef struct \
        { \
            int (*is_empty)(const struct _list_##type*); \
            size_t (*size)(const struct _list_##type*); \
            const type (*front)(const struct _list_##type*); \
            void (*push_front)(struct _list_##type*, type); \
        } _list_functions_##type; \
        \
        typedef struct _list_elem_##type \
        { \
            type _data; \
            struct _list_elem_##type* _next; \
        } list_elem_##type; \
        \
        typedef struct _list_##type \
        { \
            size_t _size; \
            list_elem_##type* _first; \
            list_elem_##type* _last; \
            _list_functions_##type* _functions; \
        } List_##type; \
        \
        List_##type* new_list_##type(); \
        bool list_is_empty_##type(const List_##type* list); \
        size_t list_size_##type(const List_##type* list); \
        const type list_front_##type(const List_##type* list); \
        void list_push_front_##type(List_##type* list, type elem); \
        \
        bool list_is_empty_##type(const List_##type* list) \
        { \
            return list->_size == 0; \
        } \
        \
        size_t list_size_##type(const List_##type* list) \
        { \
            return list->_size; \
        } \
        \
        const type list_front_##type(const List_##type* list) \
        { \
            return list->_first->_data; \
        } \
        \
        void list_push_front_##type(List_##type* list, type elem) \
        { \
            ... \
        } \
        \
        _list_functions_##type _list_funcs_##type = { \
            &list_is_empty_##type, \
            &list_size_##type, \
            &list_front_##type, \
            &list_push_front_##type, \
        }; \
        \
        List_##type* new_list_##type() \
        { \
            List_##type* res = (List_##type*) malloc(sizeof(List_##type)); \
            res->_size = 0; \
            res->_first = NULL; \
            res->_functions = &_list_funcs_##type; \
            return res; \
        }
    
    #define List(type) \
        List_##type
    
    #define new_list(type) \
        new_list_##type()
    

    Generic interface

    Here are some macros that simply call the list’s functions via the stored function pointers:

    #define is_empty(collection) \
        collection->_functions->is_empty(collection)
    
    #define size(collection) \
        collection->_functions->size(collection)
    
    #define front(collection) \
        collection->_functions->front(collection)
    
    #define push_front(collection, elem) \
        collection->_functions->push_front(collection, elem)
    

    Note that if you use the same structure to design other collections than lists, you’ll be able to use the last functions for any collections that stores the good pointers.

    Example of use

    And to conclude, a small example of how to use our new list template:

    /* Define the data structures you need */
    define_list(int)
    define_list(float)
    
    int main()
    {
        List(int)* a = new_list(int);
        List(float)* b = new_list(float);
    
        push_front(a, 5);
        push_front(b, 5.2);
    }
    

    You can use that amount of tricks if you really want to have some kind of templates in C, but that’s rather ugly (just use C++, it’ll be simpler). The only overhead will be one more pointer per instance of data structure, and thus one more indirection whenever you call a function (no cast is done, you don’t have to store void* pointers, yeah \o/). Hope you won’t ever use that :p

    Limitations

    There are of course some limitations since we are using mere text replacement macros, and not real templates.

    Define once

    You can only define each type once per compile unit, otherwise, your program will fail to compile. This can be a major drawback for example if you write a library and some of your headers contain some define_ instructions.

    Multi-word types

    If you want to create a List whose template type is made of several words (signed char, unsigned long, const bar, struct foo…) or whose template type is a pointer (char*, void*…), you will have to typedef that type first.

    define_list(int) /* OK */
    define_list(char*) /* Error: pointer */
    define_list(unsigned long) /* Error: several words */
    
    typedef char* char_ptr;
    typedef unsigned long ulong;
    define_list(char_ptr) /* OK */
    define_list(ulong) /* OK */
    

    You will have to resort to the same trick if you want to create nested lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.