error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

谁说我不能喝 提交于 2021-01-15 03:57:15

问题


I get this error in a C header file in this line :

char * getFechaHora(time_t & tiempoPuro);

In a C source code file i am including the header file and giving the function an implementation

char * getFechaHora(time_t &tiempoPuro){...}

also in my header file i am including correctly the "time.h" library.


回答1:


In C, if char * getFechaHora, this is your function and the two (time_t & tiempoPuro) are arguments you should declare the function as:

char * getFechaHora(*type* time_t, *type* tiempoPuro);

else if the second is a variable, declare as

char * getFechaHora(time_t *tiempoPuro);



回答2:


char * getFechaHora(time_t & tiempoPuro);

This is not C. C has no reference (&).




回答3:


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:

    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 tiempoPuro variable local to the getFechaHora function will change the value of the variable local to the function it was called from).

Reference: Pass by Reference



来源:https://stackoverflow.com/questions/12904091/error-expected-or-before-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!