CJ wrote:
Hello friends,
It seems to be quite common in libraries (e.g. GMP) to have typedefs
like
typedef struct {
/* stuff */
} __type_struct;
typedef __type_struct type_t[1];
I guess the advantage of this is that it allows type_t variables to be
passed by reference without an extra level of indirection.
That can also be archived with
typedef __type_struct* ptr_type_t;
The point with "typedef T type_t[1]", is allocating an object at
declaration, and yes, simultaneously hide the pointer issue in function
calls.
Is this considered good C style?
Not in my book... but ... that doesn't count for much.
Are there any problems or pitfalls to be wary of with this approach?
Hmm.. a pitfall for me, would be forgetting e.g. a mpz_t object, being a
pointer to a struct. Hiding such information, feel like obfuscation, but
others will prefer
typedef __mpz_struct mpz_t[1];
mpz_t a;
mpz_init(a);
over my preference, which is rather an API design like this
typedef __mpz_struct mpz_t;
mpz_t a;
mpz_init(&a);
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>