简体   繁体   English

取消引用指针C时的类型不完整

[英]Incomplete type when dereferencing pointer C

I have a header file which gives the definition 我有一个给出定义的头文件

typedef struct dyn_array dyn_array_t

I have a .c file which implements it 我有一个实现它的.c文件

struct dyn_array {
    size_t size;
    void* array;
};

In a different .c file I pass a pointer of this type to a function. 在另一个.c文件中,我将此类型的指针传递给函数。

bool first_come_first_serve(dyn_array_t* ready_queue){
    size_t limit = ready_queue->size; 
    //...
} 

I can't figure out what I'm doing wrong here. 我无法弄清楚我在做什么错。

The other .c file does not see the struct dyn_array definition. 另一个.c文件看不到struct dyn_array定义。 It has no understanding of the members like size . 它对size成员没有任何了解。


If more than 1 .c file needs to understand the structure, move the below to the .h file 如果需要多个.c文件来了解结构,请将以下内容移至.h文件

struct dyn_array {
  size_t size;
  void* array;
};

The alternative is to create a function that gets the member and keep the definition of dyn_array_t local in dyn_array.c . 另一种方法是创建一个获取成员的函数,并将dyn_array_t的定义保留在dyn_array.c This is information hiding - a good design goal. 这是信息隐藏 -一个好的设计目标。

// in dyn_array.h
typedef struct dyn_array dyn_array_t;
size_t get_size(const dyn_array_t* ready_queue);

// in different.c
#include <dyn_array.h>
bool first_come_first_serve(dyn_array_t* ready_queue){
  size_t limit = get_size(ready_queue); 
  //...
}

// in dyn_array.c
#include <dyn_array.h>

struct dyn_array {
    size_t size;
    void* array;
};

size_t get_size(const dyn_array_t* ready_queue) {
   return ready_queue->size; 
}

"Incomplete Type" means you have only declared / used the type, and not explained it. “不完整的类型”表示您仅声明/使用了该类型,而没有对其进行解释。 Your second .c file might #include "ah" but note that the definition of dyn_array is in ac and not ah 您的第二个.c文件可能#include "ah"但请注意dyn_array定义ac而不是ah

To fix this, both declare and define dyn_array in ah : 要解决此问题, dyn_arrayah声明并定义dyn_array

struct dyn_array {
    size_t size;
    void* array;
};

The compiler needs to complete the type definition to be able to dereference a pointer to it and access its fields. 编译器需要完成类型定义,才能取消指向它的指针并访问其字段。 You have to put the type definition in a header (possibly different than the one you used to put the typedef declaration) and include it in the second file also, to be able to access past the pointer. 您必须将类型定义放在标头中(可能与用于放置typedef声明的标头不同),并将其也包含在第二个文件中,以便能够通过指针进行访问。 The compiler allows you to access a pointer to incomplete type definition, as long as you don't need the details of that type. 编译器允许您访问不完整类型定义的指针,只要您不需要该类型的详细信息即可。 As in your second file, the compiler knows nothing about how the structure is built, so it cannot access the size field. 与第二个文件一样,编译器对结构的构造一无所知,因此无法访问size字段。

If you need some parts of your code not to be able to access the internal details of your data type, and others be completely aware of it, then prepare two header files (one public and other private) like this: 如果您需要代码的某些部分无法访问数据类型的内部详细信息,而其他部分完全了解它,则可以准备两个头文件(一个公共文件,另一个私有文件),如下所示:

dynarry.h 迪纳里

typedef struct dyn_array dyn_array_t;
size_t get_size(const dyn_array_t* ready_queue);

dynarryP.h 迪纳里

#include "dynarry.h"
struct dyn_array {
    size_t size;
    void *array;
};

and then, in the modules using as clients the library include only the "dynarry.h" , while in the internal implementation you #include "dynarryP.h" . 然后,在用作客户端的模块中,该库仅包含"dynarry.h" ,而在内部实现中,您#include "dynarryP.h" This is somewhat similar to object oriented programming, in relation to hidding internal implementation details . 关于隐藏内部实现细节 ,这有点类似于面向对象的编程。

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

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