对具有结构的不完整类型应用 sizeof 无效
我有一个结构,其中放置有关玩家的所有信息。这是我的结构:
struct player{
int startingCapital;
int currentCapital;
int startingPosition;
int currentPosition;
int activePlayer;
int canPlay;
};
这就是我的主要内容:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{ int s,i,numOfPlayers;
struct player *players;
printf("Give the number of players: \n");
scanf("%d",&numOfPlayers);
players = (struct player *)calloc(numOfPlayers,sizeof(struct player));
system("PAUSE");
return 0;
}
我要求用户提供玩家数量,然后我尝试分配所需的内存。但我遇到了这个我无法弄清楚的编译器错误:
invalid application of `sizeof' to incomplete type `player'
I have a struct where I put all the information about the players. That's my struct:
struct player{
int startingCapital;
int currentCapital;
int startingPosition;
int currentPosition;
int activePlayer;
int canPlay;
};
And that's my main:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{ int s,i,numOfPlayers;
struct player *players;
printf("Give the number of players: \n");
scanf("%d",&numOfPlayers);
players = (struct player *)calloc(numOfPlayers,sizeof(struct player));
system("PAUSE");
return 0;
}
I'm asking the user to give the number of players and then I try to allocate the needed memory. But I'm getting this compiler error that I can't figure out:
invalid application of `sizeof' to incomplete type `player'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这意味着包含
main
的文件无权访问player
结构定义(即不知道它是什么样子)。尝试将其包含在
header.h
中,或者创建一个类似构造函数的函数,如果它是一个不透明对象,则对其进行分配。编辑
如果您的目标是隐藏结构的实现,请在有权访问该结构的 C 文件中执行此操作:
但是,如果不应该隐藏实现 - 即
main
应该合法地说p->canPlay = 1
最好将结构体的定义放在header.h
中。It means the file containing
main
doesn't have access to theplayer
structure definition (i.e. doesn't know what it looks like).Try including it in
header.h
or make a constructor-like function that allocates it if it's to be an opaque object.EDIT
If your goal is to hide the implementation of the structure, do this in a C file that has access to the struct:
However if the implementation shouldn't be hidden - i.e.
main
should legally sayp->canPlay = 1
it would be better to put the definition of the structure inheader.h
.诸如“Invalid application of sizeof to incomplete type with a struct ...”之类的错误的原因总是缺少 include 语句。尝试找到合适的库来包含。
The cause of errors such as "Invalid application of sizeof to incomplete type with a struct ... " is always lack of an include statement. Try to find the right library to include.
当尝试访问未初始化的外部数组的
sizeof()
时,也会显示您的错误:请注意,如果没有
,您将收到
array size Missing
错误extern 关键字。Your error is also shown when trying to access the
sizeof()
of an non-initialized extern array:Note that you would get an
array size missing
error without theextern
keyword.在这里聚会真的很晚了,但是上面引用的这个错误原因的一个特殊情况就是简单地引用上面定义了结构的 sizeof() 结构:
Really late to the party here, but a special case of the reasons for this error cited above would simply be to reference a structure with sizeof() above where the structure is defined:
我认为问题在于您将
#ifdef
而不是#ifndef
放在header.h
文件的顶部。I think that the problem is that you put
#ifdef
instead of#ifndef
at the top of yourheader.h
file.我是初学者,可能不清楚语法。
参考上面的资料,我还是不太清楚。
两者都是“arrA”和“arrA”可以访问“arrB”(打印出来)。但是,无法获得“arrB”的大小。
那里有什么问题吗?
在我的程序中,“arrA”& “arrB”是常量列表并在编译之前固定。我想使用标签(让我易于维护并节省 RAM 内存)。
I am a beginner and may not clear syntax.
To refer above information, I still not clear.
Both "arrA" & "arrB" can be accessed (print it out). However, can't get a size of "arrB".
What is a problem there?
In my program, "arrA" & "arrB" are constant lists and fixed before to compile. I would like to use a label(let me easy to maintenance & save RAM memory).