3

opts.h:

#ifndef PINF_OPTS_H
#define PINF_OPTS_H
#endif //PINF_OPTS_H

// == DEFINE ==
#define MAX_OPTS 100

// == VAR ==
struct _opt {
    char *option; // e.g. --group
    char *alias; // e.g. -G
    int reqArg; // Require Argument | 0: No 1: Yes
    int maxArgs; // -1: Undefined/ Unlimited
    int func; /* Run Function? 0: No 1: Yes
               * If No, it can be checked with function 'isOptEnabled'
               */
} opt;

struct _optL {
    struct opt avOpt[MAX_OPTS];
} optL;

struct _acOpt {
    struct opt *acOpt[MAX_OPTS];
} acOpt;

// == FUNC ==
void initOpts(void);

opts.c:

#include "opts.h"

#include <stdio.h>
#include <stdlib.h>

// == VAR ==
static struct optL *optList;
static struct acOpt *activeOpts;

// == CODE ==
void initOpt(void) {
    optList = (struct optL *)malloc(sizeof(struct optL *));
    activeOpts = (struct acOpt *)malloc(sizeof(struct acOpt *));
}

opts_test.c:

#include <stdio.h>
#include "../include/opts.h"

int main(void) {
    initOpts();
    return 0;
}

I compile it with:

gcc -c include/opts.c && gcc -c opts_test.c && gcc -o opts_test opts_test.o opts.o; rm -f *.o;

Output:

In file included from include/opts.c:5:0:    
include/opts.h:14:16: error: array type has incomplete element type ‘struct opt’     
     struct opt avOpt[];     
                ^~~~~       
include/opts.h:28:17: error: flexible array member in a struct with no named members     
     struct opt *acOpt[];      
                 ^~~~~       

Why gcc does not compile my File?
In a other Project i used exactly this code and it worked.
Now it does not working....

5
  • 1
    struct opt is not known when you use it. Commented Dec 11, 2017 at 16:40
  • Why is this tagged as C++? Commented Dec 11, 2017 at 16:41
  • move the definition for _optL after the definition for _opt. Commented Dec 11, 2017 at 16:43
  • 1
    There is no definition of struct opt in your code. You only have a definition of struct _opt and an object of it with the name opt. struct optL and struct acOpt have the same problem. Commented Dec 11, 2017 at 16:43
  • @johnelemans I have tried it but i doesnt work Commented Dec 11, 2017 at 17:12

1 Answer 1

2

It looks like you are declaring a struct and then trying to give it another name. Try using typedef and then just use the new name without the "struct". Something like this.

Also, you are mallocing memory the size of a pointer to the struct and not the size of the struct.

// == VAR ==
typedef struct _opt {
    char *option; // e.g. --group
    char *alias; // e.g. -G
    int reqArg; // Require Argument | 0: No 1: Yes
    int maxArgs; // -1: Undefined/ Unlimited
    int func; /* Run Function? 0: No 1: Yes
               * If No, it can be checked with function 'isOptEnabled'
               */
} opt_t;


typedef struct _optL {
   opt_t avOpt[MAX_OPTS];
} optL_t;
Sign up to request clarification or add additional context in comments.

2 Comments

No it does not work. Compiler Errors: include/opts.h:24:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘struct’ typdef struct _opt opt_t; ^~~~~~ include/opts.h:27:4: error: unknown type name ‘opt_t’ opt_t avOpt[MAX_OPTS]; ^~~~~
It works fine for me. Post the exact line that is failing.

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.