Kyrol
Kyrol

Reputation: 3607

Return value of a function with a non-default type

I have a function:

*Foo* create_foo();

where Foo is a struct with many fields:

typedef struct foo {
    int progr_num;
    char* size;
    char* account;
    int matric_num;
    int codex_grp;
    char* note;
} Foo;

What is the exactly return value of this function when I call it??

the function:

Foo create_foo() {
    Foo x;
    ...
    ...
    return x
}

I know that the return type is Foo, but if I invoke the function and want to test the return value, which is the correct value?? (for example if a funcion is an int type, the return is 0 or -1).

When I call the function what is the return correct value??

for example:

int main() {
    Foo foo_check;
    foo_check = create_foo();
    if(!foo_check)
    return ... **???**
}

Upvotes: 0

Views: 136

Answers (4)

pmg
pmg

Reputation: 108978

Rather than passing the (large) struct back and forth on the stack, pass a pointer and use the stack for a status indicator.

struct foo { /* ... whatever ... */ };
int fxfoo(struct foo *pfoo) { /* ... whatever ... */ return ALLOK?0:1; }
int main(void) {
    struct foo objfoo;
    if (fxfoo(&objfoo)) /* error */;
}

Upvotes: 1

Kevin
Kevin

Reputation: 56059

You use the return value the same way you would use any return value...

Foo x = create_foo();
if(x.field == y) {...};

Upvotes: 0

poy
poy

Reputation: 10507

struct A{
int x;
char c;
}

Foo create_foo(){
struct A a;
a.x = 5;
a.c = 'd';

return a; // <-- This will be your return value.
}

Upvotes: 0

Lindydancer
Lindydancer

Reputation: 26094

The return value is a full instance of the structure Foo. If you would like to check its value, you would have to assign it to something and then check field by field.

Upvotes: 0

Related Questions