C语言 错误:';'、',' 或 ')' 在 '&' 标记之前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12904091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
提问by Youssef Khloufi
I get this error in a C header file in this line :
我在这一行的 C 头文件中收到此错误:
char * getFechaHora(time_t & tiempoPuro);
In a C source code file i am including the header file and giving the function an implementation
在 C 源代码文件中,我包含头文件并为该函数提供一个实现
char * getFechaHora(time_t &tiempoPuro){...}
also in my header file i am including correctly the "time.h" library.
同样在我的头文件中,我正确地包含了“time.h”库。
回答by ouah
char * getFechaHora(time_t & tiempoPuro);
This is not C. C has no reference (&).
这不是 C。C 没有引用 ( &)。
回答by Optimus Prime
In C, if char * getFechaHora, this is your function and the two (time_t & tiempoPuro) are arguments you should declare the function as:
在 C 中,如果 char * getFechaHora,这是您的函数,并且两个 (time_t & tiempoPuro) 是您应该将函数声明为的参数:
char * getFechaHora(*type* time_t, *type* tiempoPuro);
else if the second is a variable, declare as
否则如果第二个是变量,则声明为
char * getFechaHora(time_t *tiempoPuro);
回答by Daniel Hopkins
The problem is your use of the &symbol. Passing by reference in that way is not supported in C. To accomplish this in C, you need to pass in a pointer to the variable like so:
问题是您对&符号的使用。C 不支持以这种方式通过引用传递。要在 C 中完成此操作,您需要传入一个指向变量的指针,如下所示:
char * getFechaHora(time_t * tiempoPuro);
Technically, you are still passing by value (passing the value of the pointer), but it will accomplish the same thing (modifying the value pointed to by the tiempoPurovariable local to the getFechaHorafunction will change the value of the variable local to the function it was called from).
从技术上讲,你仍然是按值传递(传递指针的值),但它会完成同样的事情(修改函数tiempoPuro局部变量所指向的值getFechaHora将改变函数局部变量的值它是从调用)。
Reference: Pass by Reference
引用: 通过引用传递

