Define and return a struct in c

Posted by nevan on Stack Overflow See other posts from Stack Overflow or by nevan
Published on 2010-06-10T12:36:08Z Indexed on 2010/06/10 12:42 UTC
Read the original article Hit count: 200

Filed under:
|
|

I'm trying to convert some code from Javascript to c. The function creates an array (which always has a fixed number of items) and then returns the array. I've learned that in c it's not straightforward to return an array, so I'd like to return this as a struct instead. My c is not all that great, so I'd like to check that returning a struct is the right thing to do in this situation, and that I'm doing it the right way. Thanks.

typedef struct {
    double x;
    double y;
    double z;
} Xyz;

Xyz xyzPlusOne(Xyz addOne) { 

    Xyz xyz;
    xyz.x = addOne.x + 1;
    xyz.y = addOne.y + 1;
    xyz.z = addOne.z + 1;

    return xyz;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about struct