0

Is it possible to have an array of pointers of different structs that have same size ? If yes would it possible to access from the array the structs variables or members ? Can you give some examples ?

Thanks

4
  • You can have an array of generic pointers (i.e. an array of void *), but you must really know what each of the pointers point to. If you make a slight mistake you will have a bad case of undefined behavior. You might want to rethink your design instead, is my recommendation. Commented Sep 23, 2016 at 12:06
  • 1
    Also, please tell us about the actual problem you try to solve with this. That will make it easier for us to help you come up with a solution that isn't prone to errors, and that should be way more easy to maintain and understand. Please read about the XY problem, of which your question is a prime example. Commented Sep 23, 2016 at 12:07
  • 1
    Use a struct with a union of your different types, and an enum to identify which type the particular instance is. Commented Sep 23, 2016 at 12:10
  • @joachim I want to use an array to iterate through them. Commented Sep 23, 2016 at 12:12

1 Answer 1

1

Strictly speaking you can't. But you can keep pointers to base structure! When I was need "OOP" at C, i used this approach (as example):

// http://ideone.com/FhSULX

#include <stdio.h>


struct BaseFigure
{
    int type;
    const char * name;
};

struct RectangleFigure
{
    struct BaseFigure base; // must be first field!
    int width, height;
};

struct CircleFigure
{
    struct BaseFigure base; // must be first field!
    float radius;
};

enum {FIGURE_RECTANGLE, FIGURE_CIRCLE};

int main()
{
    enum {FIGURE_COUNT = 2};

    struct BaseFigure *arrayOfFigures[FIGURE_COUNT];
    struct RectangleFigure rectangle;
    struct CircleFigure circle;
    int i;

    rectangle.base.name = "rectangle";
    rectangle.base.type = FIGURE_RECTANGLE;
    rectangle.width = 10;
    rectangle.height = 20;

    circle.base.name = "circle";
    circle.base.type = FIGURE_CIRCLE;
    circle.radius = 3.5f;

    arrayOfFigures[0] = (struct BaseFigure*)&rectangle;
    arrayOfFigures[1] = (struct BaseFigure*)&circle;

    for (i = 0; i < FIGURE_COUNT; i++)
    {
        struct BaseFigure * const baseFigure = arrayOfFigures[i];
        printf("Figure type: %d, name: %s\n", baseFigure->type, baseFigure->name);
        switch (baseFigure->type)
        {
            case FIGURE_RECTANGLE:
            {
                struct RectangleFigure * const figure = (struct RectangleFigure*)baseFigure;
                printf("%s: width: %d, height: %d\n", figure->base.name,
                    figure->width, figure->height);
            }
            break;

            case FIGURE_CIRCLE:
            {
                struct CircleFigure * const figure = (struct CircleFigure*)baseFigure;
                printf("%s: radius: %f\n", figure->base.name,
                    figure->radius);
            }
            break;

            default:
                printf("Error: unknown figure with type '%d'!\n", baseFigure->type);
        }
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.