Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C89 structs

Name: Anonymous 2010-08-30 0:37

Hi /prog/. When I program C I usually just use whatever features gcc allows without really worrying about different standards. However, I am now trying to learn strict C89. I know that it's no longer "standard" C, but I want to learn it anyways.

typedef struct {
    int a, b;
} s;


So normally, if I wanted to return an s from a function I would just use return (s) {1, 2};, but apparently that's not allowed in C89. So right now I'm using
s res;
res.a = 1;
res.b = 2;
return res;


Is there a better/shorter way to do this?

Thanks.

Name: Anonymous 2010-08-30 9:34

>>11
This guy's got it correct, except his naming conventions suck. If I were you I'd also make all the pointers __restrict. You'll get faster code; just make sure you don't send the same address for multiple arguments (that's why you need in-place versions as separate functions.) Your code in >>8 becomes:

inplacecross(&b, &c);
add(&a, &b, &d)


You would implement the in-place versions as wrappers that use a temporary (because of __restrict), like this:

static __inline void inplaceadd(Vector3f* __restrict a, const Vector3f* __restrict b) {
    Vector3f out;
    add(a, b, &out);
    memcpy(a, &out, sizeof(*a));
}


Returning structs is rarely a good idea. Older compilers (pre-C89) don't even support this (though you're extremely unlikely to run into one of those today). With GCC you can set -Waggregate-return to warn if you attempt to do this.

Unfortunately, writing code with pointers instead of literal return values is going to be a bit more verbose than you'd like. That's just the way C is; don't like it, don't code in C.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List