Kvec (others?), no struct tag
jvburnes opened this issue · 2 comments
Maybe there's an alternative way to do this that makes more sense, but when I'm using kvec the struct is anonymous. Maybe this works for C++, but it drives most C compilers crazy when you need to pass kvecs by value or reference to a subroutine since only C11 even knows about anonymous structs.
I solved this by:
#define kvec(type) struct kv_##type##_s { size_t n, m; type *a; }
#define kvec_t(type) struct kv_##type##_s
Then put kvec(int);
in your headers and kvec_t(int)
in your variable definitions and parameter references.
I have the same issue!
Unfortunately, the above solution won't work for composed types, e.g. unsigned int
.
The only solution I found so far is to encapsulate the anonymous kvec
struct inside another struct.
typedef struct uints {
kvec_t(unsigned int);
} uints;
Then I can pass it around in functions as such
uints process_numbers(uints input)
{
uints output = ...
return output;
}
@aganm not only will it not work for composed types, it will also not work for some compound literals, like :
struct { int x, y; } {1,2}
(int []){1,2,3}
(int){34}
since only C11 even knows about anonymous structs
@jvburnes , isn't it a C99 feature ?
My bad. It worked on -std=c99
so I assumed this. Very odd, given this comment :
The irony is that the semantics afforded by anonymous structs and unions were available in Dennis Ritchie's 1974 C compiler, and I think gcc had supported anonymous structs and unions in the days prior to the C89 Standard. Some people seem to think it's a new feature, but it merely represents a regained ability that should never have been lost.