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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:55:29+00:00 2026-06-09T08:55:29+00:00

It is possible to do something like this How can I initialize an array

  • 0

It is possible to do something like this How can I initialize an array of pointers to structs?
but with different structs?

E.g.

static struct structA_t a = {"ads", "as"};
static struct structB_t b = {"zzds", "dfr", "shywsd"};
static struct structC_t c = {"ssa", "ad", "dhksdhs"};

struct some_type *array[] = { &a, &b, &c};

How some_type will look like?

  • 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-09T08:55:30+00:00Added an answer on June 9, 2026 at 8:55 am

    You could define some_type as a union:

    typedef union{
      struct structA_t;
      struct structB_t;
      struct structC_t;
    }some_type;
    

    This will lead you to the problem that you don’t know what’s actually contained in which element in the array.

    To overcome this, add another field specifying the content that is used:

    /* numbers to identify the type of the valid some_type element */
    typedef enum my_e_dataId{
      dataid_invalid = 0,
      dataid_a,
      dataid_b,
      dataid_c
    } my_dataId;
    
    typedef union u_data {
      struct structA_t* a;
      struct structB_t* b;
      struct structC_t* c;
    }mydata;
    
    typedef struct s_some_type{
      my_dataId dataId;
      mydata    myData;
    }some_type;
    

    Then you could initialize your array as follows:

    some_type sta[] = {
      {dataid_a, (struct structA_t*) &a},
      {dataid_b, (struct structA_t*) &b},
      {dataid_c, (struct structA_t*) &c}
    };
    

    When you loop over the elements of array, first evaluate dataId so that you know what’s contained in myData. Then, for example, access the data of the first element using

    sta[0].myData.a->FIELDNAME_OF_A_TO_ACCESS
    

    or the third element with

    sta[2].myData.c->FIELDNAME_OF_C_TO_ACCESS
    

    See this ideone for a working example: http://ideone.com/fcjuR

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

Sidebar

Related Questions

So, I can initialize a C array like this: CGFloat colors[8]= {1,0.5,0.5, 1, 0.5,0.2,0.2,
Is it possible to do something like this in Java? Object[] objArray = {
Is it possible to do something like this? public function something() { $thisMethodName =
is it possible to do something like this in JSTL: <div class=firstclass<c:if test='true'> someclass</c:if>>
Is it possible to accomplish something like this using linqtosql? select * from table
is it possible to do something like this in PHP 5.2.1? abstract class Test
Is it possible to use something like this wrapper with fluent configuration? http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/ If
Is it possible to do something like this in C#? logger != null ?
Is it possible to do something like this: INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
Is it possible to do something like this? jQuery(selector).jQuery(subselector).command(....) My code is like this:

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.