简体   繁体   English

带有指针的功能:成员引用基本类型(…)不是结构或联合

[英]Function with pointers: Member reference base type (…) is not a structure or union

I've the following error: 我遇到以下错误:

"error: member reference base type 'start' (aka 'struct no_start *') is not a structure or union". “错误:成员引用基本类型'开始'(又名'struct no_start *')不是结构或联合”。

So, I have many structures like: 因此,我有很多类似的结构:

typedef struct no_start * start;

struct no_start{
   prog * node_program;
};

And functions like this: 和这样的功能:

start * insert_start(prog * program){

   start * data = (start *) malloc(sizeof(start));

   data->node_program = program;

   return data;

}

I have a file functions.c where simple functions like this are at, a file structs.h where the structs are and the last functions.h, where i declare my functions of first file. 我有一个文件functions.c,其中有像这样的简单函数,一个文件structs.h,其中有结构,最后一个function.h,我声明了第一个文件的功能。

I dont understand why I'm having this errors. 我不明白为什么会有这个错误。 For each function I get as many errors as assigns. 对于每个函数,我都会得到与分配一样多的错误。

You do not need to type caste the return of malloc. 您无需键入cast即可返回malloc。 Doing so will create a pointer, to a pointer. 这样做将创建一个指向指针的指针。 Where as a non casted malloc call will return a pointer to the memory allocated for your structure 作为非强制转换的malloc调用将返回指向为您的结构分配的内存的指针

I wouldn't do 我不会的

typedef struct no_start * start;

ie typedef something as a pointer to a structure without doing 即,将typedef用作结构的指针,而不做

  • a typedef of the struct itself 结构本身的typedef
  • naming the pointer typedef something like start_ptr 将指针typedef命名为start_ptr之类的东西

otherwise people get confused. 否则人们会感到困惑。 As you did yourself. 正如你自己做的那样。

 start * data = (start *) malloc(sizeof(start));

assumes that start is a struct - it isnt. 假设start是一个结构-它不是。 You mean 你的意思是

 start  data = malloc(sizeof(struct no_start));

better is 更好的是

typedef struct no_start{
   prog * node_program;
} start;

typedef start * start_ptr;

     start_ptr  data = malloc(sizeof(start));

Expand the typedef and you'll see what went wrong: 展开typedef,您会发现出了什么问题:

struct no_start ** data = (struct no_start **) malloc(sizeof(struct no_start*)); 
data->node_program = program; // Nope; *data is a pointer

You could use 你可以用

start data = malloc(sizeof(*data));
data->node_program = program;

But it's usually better to avoid "pointer-typedefs", except possibly if they're used for opaque types (ie where the struct definition is hidden). 但是通常最好避免使用“ pointer-typedefs”,除非可能将它们用于不透明类型(即,隐藏结构定义的位置)。

If you dislike typing struct everywhere (which is unnecessary in C++), you can typedef the struct: 如果您不喜欢到处键入struct (在C ++中是不必要的),则可以键入def struct:

typedef struct no_start no_start;

no_start* insert_start(prog* program){
   no_start* data = malloc(sizeof(*data));
   data->node_program = program;
   return data;
}

Of course, in C++ you should use new , not malloc . 当然,在C ++中,您应该使用new而不是malloc

My mistake was to make a pointer of the struct. 我的错误是使该结构成为指针。

I had typedef struct no_start * start; 我有typedef struct no_start * start;

Instead I needed typedef struct no_start start; 相反,我需要typedef struct no_start start;

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

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