C-lite does not allow a function to return a struct?

Posted By: JamesHH

C-lite does not allow a function to return a struct? - 04/06/20 03:26

This toy example, which I believe is standard C code, is failing to compile in Zorro:


struct HIT {
int lower_barrier_hit, upper_barrier_hit;
char first_hit;
};

struct HIT compute_paths(vars x) {
struct HIT hit;

hit.lower_barrier_hit = 1;
hit.upper_barrier_hit = 2;
hit.first_hit = 'u';

return hit;
}

function run() {
vars price = series(priceClose());

struct HIT hit = compute_paths(price);
}


This gives:

Syntax error: Wrong type SETRETV:::STRUCT@16
< return hit; >
Posted By: Petra

Re: C-lite does not allow a function to return a struct? - 04/06/20 08:59

In classic C you cannot return a struct. New C/C++ versions can but believe me you normally dont want to do it. Return a struct pointer instead.

HIT* compute_paths(vars x) {
static HIT hit;
...
return &hit;
}
Posted By: Smon

Re: C-lite does not allow a function to return a struct? - 04/09/20 07:19

I have worked around that issue by updating a global struct instead of returning one.
Posted By: AndrewAMD

Re: C-lite does not allow a function to return a struct? - 04/09/20 10:31

You can also supply a pointer to a struct as an argument and fill it with values.
© 2024 lite-C Forums