Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kvec (others?), no struct tag #144

Open
jvburnes opened this issue May 27, 2020 · 2 comments
Open

Kvec (others?), no struct tag #144

jvburnes opened this issue May 27, 2020 · 2 comments

Comments

@jvburnes
Copy link

jvburnes commented May 27, 2020

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.

@aganm
Copy link

aganm commented Mar 6, 2021

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;
}

@a-p-jo
Copy link

a-p-jo commented Nov 17, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants