简体   繁体   English

返回一个结构并将一个结构传递给函数

[英]Returning a struct and passing a struct to functions

I am trying to return a struct which is defined as below: 我正在尝试返回定义如下的结构:

EXAMPLE.C
struct test
{
    int x;
    int y;
    int z;
};
struct test t1,t2;

I don't even have to explain the function, because I am getting error while declaring the function in the header file. 我什至不必解释该函数,因为在头文件中声明该函数时出错。

EXAMPLE.H
test calculate(int percent,int distance);
int modify(struct test x1);

So I will return the struct t1 in the function calculate and i will pass the struct t2 into the function modify . 因此,我将返回结构t1的功能calculate ,我将通过结构t2到函数modify I am not sure what am I doing wrong, but I am getting syntax error 我不确定自己在做什么错,但语法错误

Firstly, your struct type is called struct test . 首先,您的结构类型称为struct test Not just test , but struct test . 不只是test ,还有struct test Two words. 两个字 You used the proper type name in your modify function. 您在modify函数中使用了正确的类型名称。 You used the proper type name in the declaration of t1 and t2 . 您在t1t2的声明中使用了正确的类型名称。 Why did you suddenly shorten it to a mere test in case of calculate ? 为什么在calculate情况下突然将其缩短为仅test

Secondly, it appears that you are trying to use a yet-undeclared struct type in your function declarations, since the functions are declared in .h file and struct type is declared in .c file. 其次,似乎您正在尝试在函数声明中使用尚未声明的结构类型,因为函数是在.h文件中声明的,而结构类型是在.c文件中声明的。 Something like this can be done properly, but this is generally not a good idea (unless you are trying to implement an opaque type). 这样的事情可以正确完成,但这通常不是一个好主意(除非您尝试实现不透明的类型)。 And most likely this is not what you are trying to do. 而且很可能这不是您要尝试执行的操作。 So, why are you declaring your struct type in .c file? 那么,为什么要在.c文件中声明结构类型? A better idea would be to declare it in .h file as well, above your functions. 一个更好的主意是在函数上方的.h文件中声明它。

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

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