简体   繁体   English

将结构传递为C中的参数

[英]Passing Structure to function as argument in C

I am having this problem and Dont know the reason 我遇到这个问题,不知道原因

I have this structure 我有这个结构

typedef struct 
{

    Int32   frameID;
    Int32   slotIndx;
    Int32   symNumber;

}   recControlList;


    recControlList  *recControlListPtr;

Datatypes are typedefs. 数据类型是typedef。

Caller function is : 呼叫者功能是:

Fun( recControlListPtr);

Fun declaration is 有趣的声明是

Fun (recControlList *recControlListPtr);


syntax error : missing ')' before '*'

How to I pass structure as pointer into the function? 如何将结构作为指针传递给函数? Please help 请帮忙

You need to have the struct declaration and typedef before the declaration of Fun(). 在声明Fun()之前,您需要具有struct声明和typedef。

If all of this in the same file, then they are just in the wrong order. 如果所有这些都在同一文件中,则它们的顺序错误。 If the struct is declared in a different file, you need to #include that before the function prototype. 如果该结构是在其他文件中声明的,则需要在函数原型之前#include该结构。

EDIT : So on your second question... 编辑 :所以你的第二个问题...

The declaration is missing a return type, eg: 该声明缺少返回类型,例如:

void Fun (recControlList *recControlListPtr);

(edit: As pointed out by others, the return type in function declarations is optional in some variants of C, but it's good style and it can give you a better error message due to disambiguation). (编辑:正如其他人所指出的那样,在C的某些变体中,函数声明中的返回类型是可选的,但它的样式不错,由于歧义性,它可以为您提供更好的错误消息)。

You can refer http://fresh2refresh.com/c/c-passing-struct-to-function/ website. 您可以访问http://fresh2refresh.com/c/c-passing-struct-to-function/网站。 Very simple example program is given for Passing structures to function by address. 给出了非常简单的示例程序,用于通过地址传递结构。 I hope this will be useful for you. 希望对您有用。

You just pass it, such as in 您只需通过它,例如

char* xy = "HelloWorld";
if(!strcmp(xy, "HelloWorld"))

You can also do a 您也可以

struct xy ;
functionName(&xy);

Did you specify a return value for your function? 您是否为函数指定了返回值?

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

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