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

  • SEARCH
  • Home
  • 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 3798976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:39:37+00:00 2026-05-19T13:39:37+00:00

QUESTION ANSWERED AT END OF PAGE. FULLY WORKING CODE. Hello, I would like to

  • 0

QUESTION ANSWERED AT END OF PAGE. FULLY WORKING CODE.

Hello, I would like to do in C what I have asked in the title, however, I don’t know how to accomplish it. I have done this in C++ thanks to templates but à la C. Here is the fully functional C++ code: List.h (simple database)

*I wonder now if with void pointers I can emulate the code. The problem is that I’ve seen a link stating that void * should be avoided because it can cause more trouble than it can solve.

Basically it is a “smart-array” that stores pointers to the variables themselves.
If I know the size of each pointer and the size of each structure pointed to, simple mallocs and reallocs should do right?

typedef struct
{
  void **list;

  // internal
  int last_item_index;
  size_t element_size; // size of each pointer
  int elements;        // number of currently allocated elements
  int total_size;      // >= #elements so that we don't have to always call malloc
  int tweak_request_size; // each time the list grows we add this # of elements

} List;
// a shot at an addCopy function
// it deepcopies the object you pass in
List_addCopy(List *db, void *ptr_to_new_element)
{
  ... // grow **list
  // alloc and copy new element
  db->list[db->last_item_index+1] = malloc(element_size); // WORKS?
  // HOW TO COPY THE ELEMENT TO HERE IF IT IS A STRUCTURE FOR INSTANCE???
  ...
}

or
// a shot at an assign function 
// (allocate the elements yourself then pass the pointer to the List)
List_assign(List *db, void *ptr_to_new_element)
{
  db->List = realloc(db->List, element_size*(elements+tweak_request_size));
  db->List[db->last_item_index+1] = ptr_to_new_element;
}

// Usage example

List db; // our database
struct funky *now = (funky*)malloc(sizeof(funky));

funky->soul = JamesBrown;

List_addCopy(db, funky);

if (list[0]->soul == JamesBrown)
  puts("We did It! :D");

If I alloc everything outside and just pass the pointers to the List I guess the only problem is the void **.

Is List_add possible? Only with callbacks that do the alloc of the element and / or copy it?

Is List_assign possible? I don’t want to have a lot of work and end up with unreliable software.

Thanks a lot and sorry for the convolution in the writing :p

  • 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-05-19T13:39:37+00:00Added an answer on May 19, 2026 at 1:39 pm

    You can avoid void* with something like this:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define List(T) \
        typedef struct { \
            T** items; \
            int count;  \
        } List_ ## T ;\
        \
        List_ ## T * List_ ## T ## _New() { \
            List_ ## T * list = (List_ ## T *) malloc(sizeof(List_ ## T)); \
            list->count = 0; \
            return list; \
        } \
        \
        void List_ ## T ## _Add(List_ ## T *list, T * data) { \
            printf("%d\n", ++list->count); \
        } \
        void List_ ## T ## _Del(List_ ## T *list, int index) { \
            printf("%d\n", --list->count); \
        }
    
    /* define just one list per type */
    List(int);
    List(double);
    
    int main()
    {
        int a, b, c;
        double d, e;
        List_int *l1;
        List_double *l2;
    
        l1 = List_int_New();
        List_int_Add(l1, &a);
        List_int_Add(l1, &b);
        List_int_Add(l1, &c);
        List_int_Del(l1, 0);
        List_int_Del(l1, 0);
        List_int_Del(l1, 0);
        free(l1);
    
        l2 = List_double_New();
        List_double_Add(l2, &d);
        List_double_Add(l2, &e);
        List_double_Del(l2, 0);
        List_double_Del(l2, 0);
        free(l2);
    
        return 0;
    }
    

    That’s a poor man’s template =)

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

Sidebar

Related Questions

Based on the question asked and answered here , I have a second, more
This question have been answered in 2008. End of 2010 now. Any changes? which
I found this question which answered most of my question, however I have a
I saw a similar question asked and answered for ASP.net here How do I
I recently asked a question (and had it answered) here: jQuery Load JSON I
Edit: The below question was answered by this . I have a new updated
I don't know whether this question can be answered here, but I hope it
I have created a question an answer app for a client much like StackOverflow.
EDIT ~ I have answered my own question below in the EDIT section, not
Question Answered, see solution at end of question. More comments / answers still welcome.

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.