Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: C language declaration "bugs" |
| Date | 2016-10-11 13:58 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87d1j7avp8.fsf@bsb.me.uk> (permalink) |
| References | <90bf0ba6-5094-4551-8cc0-c60214b14362@googlegroups.com> |
Thiago Adams <thiago.adams@gmail.com> writes:
> Off course this is not a bug.
> But I think it s strange that I cannot use the same
> syntax for declarations in structs and functions.
>
> For instance:
>
> struct X
> {
> int * a[]; //ERROR
> int b[]; //ERROR
> };
The second one is not an error -- it's a flexible array member (or would
be if there were at least one valid struct member preceding it). It
does not say what you want, I agree, but it's not an error.
> void f(int * a[]) { } //Ok
> void f2(int b[]) { } //Ok
Another way of looking at is that it's a shame C permits the special
case of empty []s to mean * in a parameter declaration. It's a
hang-over from very early C and is no more than an anomaly. What's
more, you can even put a number in there for no purpose at all.
But now the []s in that position have acquired a new set of meanings:
void f(int n, int a[n]); // variably modified parameter
void f(int n, int a[*]); // alternative for (prototypes only)
void f(int a[static 3]); // pointer to at least 3 ints
void f(int a[const static 3]); // const pointer to at least 3 ints
and more besides.
The fact that type qualifiers (like const) in that position modify the
pointer is reasonable (since the pointed-to type can be qualified
anyway) but unfortunate. I.e.
void f(int a[const]);
is just a silly way to write
void f(int *const s);
<snip>
> when I do out of function scope
>
> //warning: array 'i' assumed to have one element
> int i[];
>
> So here is one more language suggestion:
>
> make empty array syntax the same of "pointer" syntax.
>
> int i[]; the same of int *i;
C's desire not to break existing code both its strength and its
weakness. It has resulted in all sorts of oddities because the ISO C
committee is very conservative about changes to the meaning of currently
valid code. Some would say it's been too conservative and some would
say not conservative enough.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
C language declaration "bugs" Thiago Adams <thiago.adams@gmail.com> - 2016-10-11 05:17 -0700
Re: C language declaration "bugs" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-11 13:58 +0100
Re: C language declaration "bugs" Thiago Adams <thiago.adams@gmail.com> - 2016-10-11 06:47 -0700
Re: C language declaration "bugs" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-11 15:03 +0100
Re: C language declaration "bugs" Thiago Adams <thiago.adams@gmail.com> - 2016-10-11 07:41 -0700
Re: C language declaration "bugs" BartC <bc@freeuk.com> - 2016-10-11 16:31 +0100
Re: C language declaration "bugs" Keith Thompson <kst-u@mib.org> - 2016-10-11 10:01 -0700
Re: C language declaration "bugs" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-11 19:10 +0100
Re: C language declaration "bugs" BartC <bc@freeuk.com> - 2016-10-11 14:14 +0100
csiph-web