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 1:42

>>9
d = add(a, cross(b, c));

Returning structs is considered poor form for pretty much this precise reason. Imagine if your vector class had dynamically allocated memory, you are going to leak that shit all over the place whenever you try and write concise code, not to mention you will end up confusing yourself when half of the vectors are on the heap and the other half are on the stack.

This style is also rather inefficient for a ray tracer, due to the number of vectors that will be created on the stack. 6-8 vectors will be created just to calculate a refracted ray, where you could do it in place with 2.

A much nicer way to do it is, in my opinion:

typedef struct {
    float x;
    float y;
    float z;
} Vector3f;

void add(const Vector3f* a, const Vector3f* b, Vector3f* c);
void inplaceadd(Vector3f* a, const Vector3f* b);
void cross(const Vector3f* a, const Vector3f* b, Vector3f* c);
void inplacecross(Vector3f* a, const Vector3f* b)

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