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

C - Union of struct and array, ugly syntax

Name: Anonymous 2013-08-16 23:57

I'm writing some floating pointer vector types in C. Using a union, I can get both struct and array representations of the same type, depending on which is more convenient. The syntax is very ugly, though:

#include <stdio.h>
#include <tgmath.h>
typedef union {
    struct { float x, y; };
    float val[2];
} vec2;
int main(int argc, char *argv[]) {
    vec2 herp = {{3, 4}}; // using {3, 4} throws an error when -Wall is enabled
    printf("Vector: {%f, %f}\n", herp.x, herp.y);
    float len = sqrt(herp.x * herp.x + herp.y * herp.y);
    printf("Length: %f\n", len);
    vec2 normalized = {{herp.val[0] / len, herp.val[1] / len}};
    printf("Normalized: {%f, %f}\n", normalized.val[0], normalized.val[1]);
}


Is there any way, using C99, C11, or possibly even GNU C extensions, that I can get syntax like the following?

vec2 derp = {6, 20}; // no need for two sets of braces in initializers
derp[0] = 5; // "anonymous" array


or am I better off making my vectors only structs or only arrays, and casting them to the other type when the alternative syntax is more convenient? (.x and .y are more convenient most of the time, but treating them as arrays will be more convenient for operations between vectors and matrices)

I know this is trivial shit but I want to get it right before I go any further.

Name: Anonymous 2013-08-18 12:05

>>37
The fact is, C++ programs are pretty much always bigger, slower an more memory hungry
Well you know C++ is typically used in LARGER projects and a lot of projects will prefer vectors and strings over cstrings and arrays. It's expected.
And C++11 only makes things worse.
C++11 makes C++ usable. Move semantics. Type inference. Lambas. Constexprs. etc.

Name: Anonymous 2013-08-18 12:54

>>41
LARGER projects [...] will prefer vectors and strings over cstrings and arrays.
Big projects should use neither C nor C++ if not absolutely necessary. This proves nothing.

Move semantics
Only needed because of a stupid C heirloom that crippled C++ as a language from the get-go.

Type inference
Should have been added as soon as the STL was made. The fact that someone thought having to write vector<type>::iterator in every for-loop on things like vectors shows that this language is designed so badly it's not even funny.

Lambdas
Are flaky as hell, utilize awful syntax as lambdas have problems with the ALGOL style and require ridiculous attention to detail because they are brought into an inferior imperative language.

Constexprs
Because C++ obviously needs even more compiler optimization keywords. It's like Fortran all over again, only worse designed.

Name: Anonymous 2013-08-18 14:24

>>42
well LE SAGER GUY, >>41 is right and you have no correct arguments against. faggot.

Name: Anonymous 2013-08-18 14:32

>>43
Of course you are right when you say nothing.

Name: Anonymous 2013-08-18 15:04

>>44

LLLLLLLLEEEEEEEEEEEEEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLL
XDXDXDD LE SAGE LELELELELEL SAGE
ISAGED DOWNVOTED UX XDXDXDXDXDXDX
NO BUMPS FOR YA XDXD LE SAGEEEDDD
TROLFLACE 9GAG LXDXDL GET BACK TO /G/EDDIT FACE XD LEEEEEEEEEEEEELLLLLLLLL
I DOWNVOTED U SAGEE XDXDXD

Name: Anonymous 2013-08-18 15:08

>>45
Back to the imagereddits with you.

Name: Anonymous 2013-08-18 15:44

>>45
You can't even imitate lelcunt well. Fuck off, the lelspam was never a /prog/ meme, like you retards like to call it.

Name: Anonymous 2013-08-18 15:46

LE SAGER GUY
Do you retards actually think this is some sort of insult? Pretty sure many imagereddit users love using sage too.

Name: DooM Guy 2013-08-18 16:05

>>46
Back to Hell with You! *Bang* *reloads shotgun*

Name: Anonymous 2013-08-18 18:25

>>48
Is this le birth of le new le/g/in meme?

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