[英]Declaring a prototype of type “struct” - C
I've been racking my brains on this for a while, I'm simply trying to create a method that returns a struct as I wish to return two int's. 我已经绞尽脑汁待了一段时间,我只是想创建一个返回结构的方法,因为我希望返回两个int。
My prototype for the method is as follows: 我的方法原型如下:
typedef struct RollDice();
Also the method itself: 方法本身也是:
typedef struct RollDice()
{
diceData diceRoll;
diceRoll.dice1 = 0;
diceRoll.dice2 = 0;
return diceRoll;
}
The compiler shows the error: "Syntax error: ')'"
for both the prototype and actual method. 编译器显示错误: "Syntax error: ')'"
用于原型和实际方法。
The struct itself: 结构本身:
typedef struct
{
int dice1;
int dice2;
}diceData;
Is it obvious where I'm going wrong? 我出错的地方很明显吗? I've tried everything I can think of. 我已经尝试了所有我能想到的东西。
Thanks 谢谢
Edit/Solution: 编辑/解决方案:
To get the program to work with the suggested solutions I had to make the following changes to the struct, 为了使程序能够使用建议的解决方案,我必须对结构进行以下更改,
typedef struct diceData
{
int dice1;
int dice2;
};
You'll want typedef struct ... diceData
to occur before your function, and then the signature of the function will be diceData RollDice()
. 你需要在你的函数之前发生typedef struct ... diceData
,然后函数的签名将是diceData RollDice()
。
typedef <ORIGTYPE> <NEWALIAS>
means that whenever <NEWALIAS>
occurs, treat it as though it means <ORIGTYPE>
. typedef <ORIGTYPE> <NEWALIAS>
表示每当<NEWALIAS>
出现时,将其视为<ORIGTYPE>
。 So in the case of what you've written, you are telling the compiler that struct RollDice
is the original type (and of course, there is no such struct defined); 因此,对于您编写的内容,您告诉编译器struct RollDice
是原始类型(当然,没有定义这样的结构); and then it sees ()
where it was expecting a new alias. 然后它看到()
它期待一个新的别名。
This is just a concrete version of Mark Rushakoff's answer: 这只是Mark Rushakoff答案的具体版本:
typedef struct
{
int dice1;
int dice2;
} diceData;
diceData RollDice()
{
diceData diceRoll;
diceRoll.dice1 = 0;
diceRoll.dice2 = 0;
return diceRoll;
}
You can't use typedef to define a function. 您不能使用typedef来定义函数。
Typedef your struct first like 首先输入你的结构
typedef struct
{
int dice1;
int dice2;
} diceData;
Then declare your function as 然后将您的函数声明为
diceData RollDice()
{
diceData diceRoll;
diceRoll.dice1 = 0;
diceRoll.dice2 = 0;
return diceRoll;
}
That declares RollDice as a function that returns a diceData struct. 它将RollDice声明为一个返回diceData结构的函数。
An alternative way to deal with trying to return two values would be to use out parameters. 处理尝试返回两个值的另一种方法是使用out参数。
In that case your function would return a boolean (to indicate success or failure) and take two pointers to integers as parameters. 在这种情况下,您的函数将返回一个布尔值(表示成功或失败),并将两个指向整数的指针作为参数。 Within the function you'd populate the contents of the pointers, like this: 在函数中,您将填充指针的内容,如下所示:
bool_t rollDice(int *pDice1, int *pDice2)
{
if (pDice1 && pDice2)
{
*pDice1 = 0;
*pDice2 = 0;
return TRUE;
}
else
{
return FALSE;
}
}
int main(int argc, char **argv)
{
int a, b;
rollDice(&a, &b);
return 0;
}
The problem is you haven't given the method a return type. 问题是你没有给方法一个返回类型。 It looks like you should be returning a dicedata
type so the prototype signature would look like 看起来你应该返回一个dicedata
类型,所以原型签名看起来像
struct dicedata RollDice();
And the method 和方法
struct dicedata RollDice()
{
diceData diceRoll;
diceRoll.dice1 = 0;
diceRoll.dice2 = 0;
return diceRoll;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.