C编程:取消引用指针不完全类型错误 [英] C programming: Dereferencing pointer to incomplete type error

查看:265
本文介绍了C编程:取消引用指针不完全类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构定义为:

struct {
 char name[32];
 int  size;
 int  start;
 int  popularity;
} stasher_file;

和指针数组这些结构:

struct stasher_file *files[TOTAL_STORAGE_SIZE];

在我的code,我正在做一个指向结构并设置其成员,并将其添加到数组:

In my code, I'm making a pointer to the struct and setting its members, and adding it to the array:

 ...
 struct stasher_file *newFile;
 strncpy(newFile->name, name, 32);
 newFile->size = size;
 newFile->start = first_free;
 newFile->popularity = 0;
 files[num_files] = newFile;
 ...

我收到一个错误:提领指向不完全类型每当我尝试访问内部 NEWFILE 成员。我在做什么错了?

I'm getting an "error: dereferencing pointer to incomplete type" whenever I try to access the members inside newFile. What am I doing wrong?

推荐答案

您还没有通过你的第一个定义来定义结构stasher_file 。你所定义的是一个的无名的结构类型和类型的变量 stasher_file 。既然在你的code为这种类型定义为结构stasher_file ,编译器会抱怨不完全型。

You haven't defined struct stasher_file by your first definition. What you have defined is an nameless struct type and a variable stasher_file of that type. Since there's no definition for such type as struct stasher_file in your code, the compiler complains about incomplete type.

为了定义结构stasher_file ,你应该做如下它

In order to define struct stasher_file, you should have done it as follows

struct stasher_file {
 char name[32];
 int  size;
 int  start;
 int  popularity;
};

请注意其中 stasher_file 的名字被放置在定义。

Note where the stasher_file name is placed in the definition.

这篇关于C编程:取消引用指针不完全类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆