P
Phil Carmody
Shao Miller said:<snip example>Shao Miller said:On 2/19/2012 00:15, Jonathan Leffler wrote:
Inside a function, you can do such initializations (under C99 - not
under C89 with GCC and -pedantic), though an array of dimension 1 is
modestly pointless.
Actually, I think that some folks use an '[1]' for 'struct's and
union's in their code just for the sake of using only the '->'
operator throughout their code.
I've also seen in used in some APIs. You get a sort of automatic call
by reference and copying (by assignment) is prohibited:
typedef struct { /* stuff */ } MyType[1];
MyType a, b;
/* ... */
a = b; /* illegal */ *a = *b; /* legal */
mytype_copy(a, b); /* no need for&a on the target */
I don't know if I like it or hate it! To decide, I think I'd have to
maintain a large body of code that uses such an API. That usually
sorts out the good ideas from the bad ones.
Yeah.
On another note, 'clang -ansi -pedantic' appears to be ok with the
following code, but 'gcc -ansi -pedantic' doesn't (4.3.3):
#include <stdio.h>
struct s_foo { unsigned char bytes[2]; };
typedef struct s_foo foo_t[1];
struct s_foo_wrapper { foo_t foo; };
int main(void) {
register const struct s_foo_wrapper reg_foo =
{ { { { 42, 42 } } } };
struct s_foo_wrapper normal_foo;
normal_foo = reg_foo;
printf(
"%d and %d\n",
normal_foo.foo->bytes[0],
I would hope that would be UB, but it seems to sneak past the combination
of 4.p2 (giving precise meaning to "shall" and "shall not") and 6.7.1.p6's
(informative) footnote 121 ("the only operators that can be applied...",
which appears to be at least morally violated).
Phil
--
Pics or it didn't happen.I'd argue that there is much evidence for the existence of a God.
-- Tom (/. uid 822)