-1

I need to do something like this:

typedef struct a A;
typedef struct b B;


struct a{
    B oi;
};

struct b{
    A ola;
};

But it returns this error when I try to compile:

gustavo@depaula-ubuntu:~/Desktop/grafo$ gcc test.c 
test.c:3:12: error: field ‘ola’ has incomplete type
   struct a ola;
            ^

EDIT: I don't think this is a XY problem, but I need the above code for this (it's not working):

typedef struct union util;
typedef struct graph Graph;
typedef struct vertex_struct Vertex;
typedef struct arc_struct Arc;

typedef struct graph{
    Vertex * vertices;
} Graph;

typedef struct vertex_struct {
    struct arc_struct * arcs;
    int name;
    struct util u, v, w, x, y, z;
};

typedef struct arc_struct {
    struct vertex_struct * tip;
    struct arc_struct * next;
    struct util a, b;
};

struct union {
    struct vertex_struct * V;
    struct arc_struct * A;
    struct graph_struct * G;
    char * S;
    int I;
};
depaulagu
  • 139
  • 2
  • 7
  • What is "something like this"? – MikeCAT Jun 05 '16 at 03:14
  • No, I don't want to do a linked list. – depaulagu Jun 05 '16 at 03:15
  • 2
    Why do you need this? Isn't this [The XY Problem](http://xyproblem.info/)? – MikeCAT Jun 05 '16 at 03:18
  • Don't ask how to do impossible things, [it's turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_). Instead state what you actually want to accomplish. – too honest for this site Jun 05 '16 at 03:22
  • While it is possible to have two different structures that *refer* to each other, e.g. with pointers, it obviously makes no sense to try to nest instances of each in the other. Assuming at least one of them contains some additional field, the infinite nesting would require infinite memory and an infinite address space, right? Instead, try to do something that actually makes sense. – Tom Karzes Jun 05 '16 at 05:56

1 Answers1

1

You cannot do what you are trying.

One of the members has to be a pointer.

struct a{
    B* oi;
};

struct b{
    A ola;
};

or

struct b{
    A* ola;
};

struct a{
    B oi;
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270