1
typedef struct unit
{
struct unit * next;

int year;
int month;
int day;
struct unit revisions[3];
char subject[100];
}schedule;

The above code is giving me the following error:

array type has incomplete element type
 struct unit revisions[3];

I'm guessing the problem is that a struct cannot contain an array of itself? If so, how can I achieve similar functionality?

2 Answers2

4

your question contains the answer itself. struct unit * next;

You can always use a pointer to the structure type inside the structure definition, and from your function, allocate memory and use it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

This would be a good workaround:

typedef struct unit{
    struct unit * next;
    int year;
    int month;
    int day;
    struct unit *revisions; //just like you do with struct unit *next
    char subject[100];
}schedule;

schedule s;
s.revisions = malloc(3 * sizeof *s.revisions);
RockOnRockOut
  • 751
  • 7
  • 18
  • 3
    [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845). Also look how you can use `sizeof` in a less error-prone way. – Deduplicator Oct 20 '14 at 15:31
  • 1
    @Deduplicator Oh my! I'm learning some good practices each day! How would I use `sizeof` "in a less error-prone way" though? would it be something like `malloc(3 * sizeof s.revisions)`? – RockOnRockOut Oct 20 '14 at 15:45
  • Thanks, I'd gone ahead and did something similar on my own. Hopefully this will help other newbies xD – Sky Lightna Oct 21 '14 at 00:16