user5603723
user5603723

Reputation: 193

Returning a struct and passing a struct to functions

I am trying to return a struct which is defined as below:

EXAMPLE.C
struct test
{
    int x;
    int y;
    int z;
};
struct test t1,t2;

I don't even have to explain the function, because I am getting error while declaring the function in the header file.

EXAMPLE.H
test calculate(int percent,int distance);
int modify(struct test x1);

So I will return the struct t1 in the function calculate and i will pass the struct t2 into the function modify. I am not sure what am I doing wrong, but I am getting syntax error

Upvotes: 0

Views: 52

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320391

Firstly, your struct type is called struct test. Not just test, but struct test. Two words. You used the proper type name in your modify function. You used the proper type name in the declaration of t1 and t2. Why did you suddenly shorten it to a mere test in case of calculate?

Secondly, it appears that you are trying to use a yet-undeclared struct type in your function declarations, since the functions are declared in .h file and struct type is declared in .c file. Something like this can be done properly, but this is generally not a good idea (unless you are trying to implement an opaque type). And most likely this is not what you are trying to do. So, why are you declaring your struct type in .c file? A better idea would be to declare it in .h file as well, above your functions.

Upvotes: 4

Related Questions