Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #24674

Re: ridiculous warning or not?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: ridiculous warning or not?
Date 2012-08-20 18:59 -0700
Organization None to speak of
Message-ID <lnharxhune.fsf@nuthaus.mib.org> (permalink)
References <f87cd21e-eec8-4085-93c9-9315da17e455@googlegroups.com> <0.2721fc1ff3b8371a7969.20120821014136BST.87628d5b5r.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> kerravon <kerravon@w3.to> writes:
[...]
>> char f[];
[...]
>> "foo.c", line 1: warning: null dimension: f
>
> The warning is true, but possibly misleading.  What you've written could
> certainly be called a "null dimension" but the effect should be as if
> you had written
>
>   char f[1];
>
> The C standard gives meaning to this bizarre syntax for what I can only
> imagine are historical reasons.  There's no sensible reason to write
> such code.
>
> This makes it probable that others, who have suggested you did not mean
> what you wrote, are right.  One way or the other, you should stop using
> this syntax because it either does not mean what you want, or (vanishing
> unlikely) it's a confusing way to write what you do want (why would you
> want am external one-element array?).

As far as I can tell, the rule that `char f[];` is treated as `char
f[1];` isn't even stated explicitly.

N1570 6.9.2p2, External definitions, says:

    A declaration of an identifier for an object that has file scope
    without an initializer, and without a storage-class specifier or
    with the storage-class specifier static, constitutes a *tentative
    definition*. If a translation unit contains one or more tentative
    definitions for an identifier, and the translation unit contains
    no external definition for that identifier, then the behavior
    is exactly as if the translation unit contains a file scope
    declaration of that identifier, with the composite type as of
    the end of the translation unit, with an initializer equal to 0.

An example (non-normative) says:

    If at the end of the translation unit containing

        int i[];

    the array i still has incomplete type, the implicit initializer
    causes it to have one element, which is set to zero on program
    startup;

So this:

    char f[];

is treated as:

    char f[] = 0;

which causes `f` to inherit its size from the initializer.

Except that

    char f[] = 0;

is invalid.  6.7.9p16 says:

    Otherwise, the initializer for an object that has aggregate or union
    type shall be a brace-enclosed list of initializers for the elements
    or named members.

A twist: That "shall" is not in a constraint, so it's undefined
behavior, not something that requires a diagnostic.

Apparently we have to interpret the "initializer equal to 0" to mean
`{ 0 }` rather than `0` in this case.  And we're supposed to infer
from that that `f` has exactly one element; apparently `{ 0 }` is
"equal to 0" but `{ 0, 0 }` isn't.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ridiculous warning or not? kerravon <kerravon@w3.to> - 2012-08-20 02:41 -0700
  Re: ridiculous warning or not? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-20 07:55 -0400
  Re: ridiculous warning or not? army1987 <army1987@ask-for-it.invalid> - 2012-08-20 19:11 +0000
  Re: ridiculous warning or not? Kenneth Brody <kenbrody@spamcop.net> - 2012-08-20 16:09 -0400
    Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 07:29 -0700
  Re: ridiculous warning or not? Andrew Cooper <amc96@cam.ac.uk> - 2012-08-21 00:36 +0100
  Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-21 01:41 +0100
    Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-08-20 18:59 -0700
      Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 08:03 -0700
        Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-09-07 14:37 -0700
          Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 19:17 -0700
            Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-09-07 19:56 -0700
              Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-08 06:30 -0700
    Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 07:57 -0700
      Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 00:25 +0100
        Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 18:58 -0700
          Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-09-07 19:26 -0700
            Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-08 06:26 -0700
              Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-09-08 11:33 -0700
                Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 14:39 -0800
                Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-12-17 15:56 -0800
                Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 10:31 -0800
                Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-12-20 16:57 -0800
                Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-21 03:11 +0000
                Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 09:06 -0800
          Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 03:42 +0100
            Re: ridiculous warning or not? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-08 06:01 -0700
              Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 14:45 +0100
                Re: ridiculous warning or not? Keith Thompson <kst-u@mib.org> - 2012-09-08 11:28 -0700
                Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 22:53 +0100
  Re: ridiculous warning or not? kerravon <kerravon@w3.to> - 2012-08-21 03:25 -0700
    Re: ridiculous warning or not? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-21 11:20 -0700
      Re: ridiculous warning or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-21 21:07 +0100
        Re: ridiculous warning or not? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-21 14:39 -0700
          Re: ridiculous warning or not? Ike Naar <ike@iceland.freeshell.org> - 2012-08-21 22:09 +0000
            Re: ridiculous warning or not? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-21 15:13 -0700
              Re: ridiculous warning or not? Fred K <fred.l.kleinschmidt@gmail.com> - 2012-08-22 07:17 -0700

csiph-web