0

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?

error message during compilation-time

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;
}
5
  • 4
    Is struct dswap_opt_low(int inp_1; int inp_2;); supposed to be a function declaration, or a variable definition? It's neither. Commented Sep 16, 2019 at 11:29
  • 1
    Please learn how to create a minimal reproducible example to show us, one that replicates the exact error you show. While it's easy to figure out in this case, mismatching error and code could lead us astray. Commented Sep 16, 2019 at 11:29
  • @WeatherVane Is dsawp_opt_low(int inp_1, int inp_2); legal? Commented Sep 16, 2019 at 11:32
  • 1
    If you add a proper return-type then that would be a valid forward declaration of a function. Commented Sep 16, 2019 at 11:33
  • This struct dswap_opt_low(int inp_1; int inp_2;); and this _struct dswap_opt_low(int inp_1, int inp_2;); is not the same. Commented Sep 16, 2019 at 11:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I did, however now it returns 2 error messages. One for dswapoptlow.h:4.27, that there is no expected identifier or( before int. The second one is for dswaptoplow.h:6:1, that there is no expected-qualifier-list before typedef.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.