1

Is there an option to create a generic structure, where providing a data type creates the corresponding structure?

For example:

typedef struct StbInt {
    int value;
    struct Stb *left;
    struct Stb *right;
} StbInt;

and

typedef struct StbChr {
    char value;
    struct Stb *left;
    struct Stb *right;
} StbChr;

Only the value variable has a different data type. Do I have to create two different structures?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
newhere
  • 137
  • 8
  • 1
    I think you are looking for a `union`, e.g. http://stackoverflow.com/questions/5842441/how-to-use-union-in-c-language – mustaccio Jan 25 '15 at 18:35
  • Note that the `struct Stb *` members in the structures shown are pointers to a type unrelated to the `struct StbInt` and `struct StbChr` types shown in the question. Or, more plausibly, they're typos for `struct StbChr *` and `struct StbInt *`. – Jonathan Leffler Jan 25 '15 at 18:36

5 Answers5

2

If you want to have some generality I would suggest using a void* data member, like this:

typedef  struct Stb{
    void*  value;
    size_t size_of_value;
    struct Stb *left;
    struct Stb *right;
}Stb;

That way you don't have to replicate code. You can see I added a data member named size_of_value, which should get the value of the data you are storing.

I would suggest you take a look at this question about generic tree in C.


"How can I point value to int or a char?" See the following two links:

How do you convert void pointer to char pointer in C

How to cast an integer to void pointer?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

You can use a union to do something like that:

typedef struct foo
{
  struct Stb *left;
  struct Stb *right;
  union bar
  {
    int intvalue;
    char *svalue;
  } bar;
} foo;

You may want another struct field determining what the type is.

abligh
  • 24,573
  • 4
  • 47
  • 84
0

The closest thing I can think of to an abstract data-structure would be to fake it or to use a union.

In the case of faking it, chars can be looked at as really small ints so you could easily pack the char value into your int struct, or just use a void pointer.

typedef  struct StbVar {
    void *value;
    struct Stb *left;
    struct Stb *right;
};

As for a union you could union either the value or type it self. (In both cases you'll have to unpack the union to use the data.

// Union of types
union StbVar { 
    StbChr* c;
    StbChr* i;
};

// Union of values
typedef  struct StbVar {
    union {
        char character;
        int integer;
    };
    struct Stb *left;
    struct Stb *right;
};
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
zellio
  • 31,308
  • 1
  • 42
  • 61
0

On this specific case, the two structure types are likely to have the same size because of padding after the char member. So instead of two different structure types, just use your struct StbInt for both uses, it will be more readable than to use useless union here.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

If your data is small (f.e. double, or int, or char), you can use union.

For large data you can use void* pointer on, and allocate a memory dynamically.

There is a little hack to simplify this method:

typedef struct UniversalNode {
    struct UniversalNode* left;
    struct UniversalNode* right;
    char data[1];
}

Then you can allocate memory to store two pointers and all necessary data:

UniversalNode* node = (UniversalNode*)malloc(2 * sizeof(void*) + DATA_SIZE);
Data* data = (Data*)&(node->data);

DATA_SIZE may be different for different nodes.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29