Reputation: 616
I do not understand what identifier is excepted in my dsoptlow.h.
I was practicing creating function declarations in headers. This should swap 2 values using only 2 variables and returning same variables, but with swapped values.
However during compilation this error message is shown and I have no idea what I got wrong or mistyped:
error: expected identifier or '(' before 'int'
Also, if you can give a better version for my function to return multiple values it would be very much appreciated.
And last but not least, does a type defined in a function definition return that type in main function? So does it return a struct
I defined as a new type, or integer?
The following code is my header.
#ifndef _dswapoptlow_h
#define _dswapoptlow_h
struct dswap_opt_low(int inp_1; int inp_2;);
#endif
The following code is the function definition.
//dswapoptlow.c src file
#include "dswapoptlow.h"
struct _return{int a;int b;}; //Init a struct named _return for 2 integer variables.
typedef struct _return _struct;
_struct dswap_opt_low(int inp1, int inp2)
{
_struct _instance;
_instance.a=inp1;
_instance.b=inp2;
_instance.a=_instance.a+_instance.b;
_instance.b=_instance.a-_instance.b;
_instance.a=_instance.a-_instance.b;
return _instance;
}
Upvotes: 0
Views: 3050
Reputation: 8614
dswap_opt_low
in the header is a function declaration. The function returns a struct _return
and takes two parameters.
The correct syntax is
struct _return dswap_opt_low(int inp_1, int inp_2);
Also, you can move the definition and typedef of _struct
into the header so that this is visible there. Then you can use
struct _return{int a;int b;}; //Init a struct named _return for 2 integer variables.
typedef struct _return _struct;
_struct dswap_opt_low(int inp_1, int inp_2);
Note that it is a bad design practice to use _
in the first character of an identifier. I would suggest that you change the name and use more descriptive types.
Additional Note - Your function dswap_opt_low
returns a local variable _instance
. If the returned value is used elsewhere in the program it will result in undefined behaviour and you will get unpredictable results.
Upvotes: 1
Reputation: 41046
The message is quite confusing (there should be another error), but your function is returning a struct
and the compiler doesn't know the size of this struct
Move these two lines:
struct _return{int a;int b;}; //Init a struct named _return for 2 integer variables.
typedef struct _return _struct;
from .c
to .h
Upvotes: 1