Hi,

Simple but driving me nuts.

I am trying to create a table of pointers to already defined structures. But I can't find correct directions and I'm not used to this so HELP pliz!

I managed to do it for functions, for some reason I can't find how to do it for structs.

Imagine I have

struct a
{
    uint8_t   a1;
    uint16_t a2;
}

struct b
{
 ...
}

struct c
{
...
}

I also have 3 defines of indexes which will allow me to get the right structure I need using the table (array):

#define TYPEA  1
#define TYPEB  2
#define TYPEC  3

Now what should the typedef (if I even need one), something like:

typedef (*my_struct) ... ?;

And is the table :

const my_struct my_table[]=
{
    [TYPEA] = (my_struct)a;
    [TYPEB] = (my_struct)b;
    [TYPEC] = (my_struct)c;
}

???

I'm still learning the intricate C ways so please give me a hand :)

Bye
T-

I get the impression that you're looking for a variant type. C isn't good at that, so you might consider something more along these lines:

#include <stdio.h>

#define TYPEA 0
#define TYPEB 1

struct my_struct {
  int type;
  void *data;
};

struct a {
  double x;
};

struct b {
  int x;
};

int main ( void )
{
  struct a foo = { 1.23 };
  struct b bar = { 123 };
  struct my_struct my_array[] = {
    { TYPEA },
    { TYPEB }
  };

  my_array[TYPEA].data = &foo;
  my_array[TYPEB].data = &bar;

  printf ( "TYPEA: %f\nTYPEB: %d\n", 
    ( (struct a*)my_array[TYPEA].data )->x,
    ( (struct b*)my_array[TYPEB].data )->x );

  return 0;
}

A pointer to void is the generic type in C, but it can be awkward to use in some cases.

Thanks for the reply,

But I still have one question: how can you explain the fact that you can fill the array this way?:

struct my_struct my_array[] = {
    { TYPEA },
    { TYPEB }
  };

without nothing for the data field?

And the one reason I needed pointers to structures, is to be able to call them to get a size, and not fill them immediately. Do you think that can be done with this method?

Thanks for the reply,

But I still have one question: how can you explain the fact that you can fill the array this way?:

struct my_struct my_array[] = {
    { TYPEA },
    { TYPEB }
  };

without nothing for the data field?

And the one reason I needed pointers to structures, is to be able to call them to get a size, and not fill them immediately. Do you think that can be done with this method?

If that's a problem then try:

struct my_struct my_array[] = {
    { TYPEA, NULL },
    { TYPEB, NULL }
  };

>how can you explain the fact that you can fill the array this way?
The members I didn't explicitly initialize get default initialized to 0. In the case of a pointer, zero initialization is a null pointer.

>Do you think that can be done with this method?
I want to say yes, but your question is vague enough that I can't be sure. When you say you want to get a size, do you mean a size of the array (such as a dynamic array)?

This article has been dead for over six months. Start a new discussion instead.