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 462549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:05:08+00:00 2026-05-12T23:05:08+00:00

I have a list in which i want to be able to put different

  • 0

I have a list in which i want to be able to put different types. I have a function that returns the current value at index:

void *list_index(const List * list, int index) {
    assert(index < list->size);
    return list->data[index];
}

In the array there are multiple types, for example:

typedef struct structA { List *x; char *y; List *z; } structA;
typedef struct structB { List *u; char *w; } structB;

Now in order to get data from the array:

structA *A;
structB *B;
for(j=0... ) {
 A = list_index(list, j);
 B = list_index(list, j);
}

But now how do I find out the type of the return value? Is this possible with typeof (I’m using GCC btw)?

And is this even possible or do i have to make some sort of different construction?

  • 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-12T23:05:08+00:00Added an answer on May 12, 2026 at 11:05 pm

    C handles types and typing entirely at compile time (no dynamic typing), so once you’ve cast a pointer to a ‘void *’ its lost any information about the original type. You can cast it back to the original type, but you need to know what that is through some other method.

    The usual way to do this is with some kind of type tag or descriptor in the beginning of all the objects that might be stored in your list type. eg:

    typedef struct structA { int tag; List *x; char *y; List *z; } structA;
    typedef struct structB { int tag; List *u; char *w; } structB;
    enum tags { structAtype, structBtype };
    

    You need to ensure that every time you create a structA or a structB, you set the tag field properly. Then, you can cast the void * you get back from list_index to an int * and use that to read the tag.

    void *elem = list_index(list, index)
    switch (*(int *)elem) {
    case structAtype:
        /* elem is a structA */
                  :
    case structBtype:
        /* elem is a structB */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of images which I want to display in a page.
I have a list which has repeating items and I want a list of
I have a list of objects which I want to turn into a set.
I have a List which contains a list of objects and I want to
I have a list of person objects which I want to send in response
I have a list of URLs from which I want to scrape an attribute.
I have a list of files of which I want to add a line
I have a list of e-mails from one database which I want to check
I have a collection (or list or array list) in which I want to
Say I have a list x with unkown length from which I want to

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.