简体   繁体   English

没有命名成员的GCC错误结构灵活数组成员

[英]GCC Error struct flexible array member with no named members

opts.h: 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: 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: 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; gcc -c include / opts.c && gcc -c opts_test.c && gcc -o opts_test opts_test.o opts.o; rm -f *.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? 为什么gcc无法编译我的文件?
In a other Project i used exactly this code and it worked. 在另一个项目中,我恰好使用了此代码,并且可以正常工作。
Now it does not working.... 现在不起作用...

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". 尝试使用typedef,然后仅使用不带“ 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM