3 ms·
Worth noting that you can always wrap an array in a structure and kick it around in your program happily, as if it's how the language should've been. struc
by mojuba 10y ago
Worth noting that you can always wrap an array in a structure and kick it around in your program happily, as if it's how the language should've been.
struct { int a[5]; } arr5 = {{4, 3, 2}};
What's a bit weird though, is that while initialization like above is possible, assignment of a contsant is not as smooth in C, you will have to typedef your structure and use it in a typecast expression:
typedef struct { int a[5]; } Array5;
...
arr5 = (Array5){{4, 3, 2}};
- gpderetta 10y agoYou can actually drop the extra brackets (at least in C++, but IIRC also in C). C++11 has array<T, size_t> which has proper value semantics. It is implemented exactly like your structure above, so no overhead, and also has a proper operator== and assignment via initializer list.