I can’t get the code below to compile (see errors). Advice on correction would be appreciated.
#include <stdio.h>
typedef struct {
char *fldName;
unsigned fldLen;
} Field;
typedef struct {
char *fldPrompt;
unsigned startRow;
unsigned startCol;
} Prompt;
typedef struct {
Field *fields[];
Prompt *prompts[];
unsigned numFlds; <<< invalid field declaration after empty field
} Form; <<< in '(incomplete) struct (no name)'.
Field firstName = { "fName", 12 };
Field surName = { "sName", 25 };
Field gender = { "gder", 1 };
Prompt fn = { "First Name : ", 4, 10 };
Prompt sn = { "Surname : ", 6, 10 };
Prompt gn = { "Gender : ", 8, 10 };
int main (void)
{
Form aForm = { { &firstName, &surName, &gender },
{ &fn, &sn, &gn},
3 }; <<< Multiple initializers for the same element
return 0; <<< Too many initializers
}
All your errors stem from the fact that you incorrectly declare arrays inside your struct. You have to specify the size of the array; you cannot just use empty brackets. I.e. this would work:
If you need to allow for varying number of elements, you’ll have to use something else. For example, you can have both fields be pointers:
But then you’ll have to dynamically allocate and free memory for them, and you definitely won’t be able to use aggregate initializer to initialize the struct.