Varibly modified type in switch statement

Discussion in 'C Programming' started by Zhang Yuan, Dec 24, 2012.

  1. Zhang Yuan

    Zhang Yuan Guest

    When I read c standard ISO/IEC 9899:201x
    I'm confused about the sentence below ,why we make the switch statement
    within the scope of that variably modified type identifier.
    Thanks in advance if any one can offer some code example or explanation.

    ------
    If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.154)

    154) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.

    ------
    Zhang Yuan, Dec 24, 2012
    #1
    1. Advertising

  2. On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:

    > When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
    > below ,why we make the switch statement within the scope of that
    > variably modified type identifier. Thanks in advance if any one can
    > offer some code example or explanation.
    >
    > ------
    > If a switch statement has an associated case or default label within
    > the scope of an identifier with a variably modified type, the entire
    > switch statement shall be within the scope of that identifier.154)
    >
    > 154) That is, the declaration either precedes the switch statement,
    > or it follows the last case or default label associated with the
    > switch that is in the block containing the declaration.
    >
    > ------


    The paragraph makes constructs like this illegal:

    void foo()
    {
    int n = bar();
    switch(baz())
    {
    case 1:
    char s[n]; // Variably modified type
    // do something
    break;
    default:
    // do something else
    }
    }

    The reason for making this invalid is because 's' is also in scope in the
    default branch of the switch statement, but the control flow did not pass
    through the declaration, so the program did not have the chance to
    calculate how much room to reserve for `s`.
    To make this legal, you would have to change it to

    void foo()
    {
    int n = bar();
    char s[n]; // Variably modified type
    switch(baz())
    {
    case 1:
    // do something
    break;
    default:
    // do something else
    }
    }

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Dec 24, 2012
    #2
    1. Advertising

  3. Zhang Yuan

    Zhang Yuan Guest


    > The reason for making this invalid is because 's' is also in scope in the
    >
    > default branch of the switch statement, but the control flow did not pass
    >
    > through the declaration, so the program did not have the chance to
    >
    > calculate how much room to reserve for `s`.
    >
    > To make this legal, you would have to change it to



    Thanks,really appreciate your explicit explanation.
    Zhang Yuan, Dec 24, 2012
    #3
  4. Zhang Yuan

    Philip Lantz Guest

    Bart van Ingen Schenau wrote:
    > Zhang Yuan wrote:
    > > When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
    > > below ,why we make the switch statement within the scope of that
    > > variably modified type identifier. Thanks in advance if any one can
    > > offer some code example or explanation.
    > >
    > > ------
    > > If a switch statement has an associated case or default label within
    > > the scope of an identifier with a variably modified type, the entire
    > > switch statement shall be within the scope of that identifier.154)
    > >
    > > 154) That is, the declaration either precedes the switch statement,
    > > or it follows the last case or default label associated with the
    > > switch that is in the block containing the declaration.
    > >
    > > ------

    >
    > The paragraph makes constructs like this illegal:
    >
    > void foo()
    > {
    > int n = bar();
    > switch(baz())
    > {
    > case 1:
    > char s[n]; // Variably modified type
    > // do something
    > break;
    > default:
    > // do something else
    > }
    > }
    >
    > The reason for making this invalid is because 's' is also in scope in the
    > default branch of the switch statement, but the control flow did not pass
    > through the declaration, so the program did not have the chance to
    > calculate how much room to reserve for `s`.
    > To make this legal, you would have to change it to
    >
    > void foo()
    > {
    > int n = bar();
    > char s[n]; // Variably modified type
    > switch(baz())
    > {
    > case 1:
    > // do something
    > break;
    > default:
    > // do something else
    > }
    > }


    Or (my preference):

    void foo()
    {
    int n = bar();
    switch(baz())
    {
    case 1: {
    char s[n]; // Variably modified type
    // do something
    break;
    }
    default:
    // do something else
    }
    }
    Philip Lantz, Dec 25, 2012
    #4
  5. Zhang Yuan

    Tim Rentsch Guest

    Bart van Ingen Schenau <> writes:

    > On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:
    >
    >> When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
    >> below ,why we make the switch statement within the scope of that
    >> variably modified type identifier. Thanks in advance if any one can
    >> offer some code example or explanation.
    >>
    >> ------
    >> If a switch statement has an associated case or default label within
    >> the scope of an identifier with a variably modified type, the entire
    >> switch statement shall be within the scope of that identifier.154)
    >>
    >> 154) That is, the declaration either precedes the switch statement,
    >> or it follows the last case or default label associated with the
    >> switch that is in the block containing the declaration.
    >>
    >> ------

    >
    > The paragraph makes constructs like this illegal:
    >
    > void foo()
    > {
    > int n = bar();
    > switch(baz())
    > {
    > case 1:
    > char s[n]; // Variably modified type
    > // do something
    > break;
    > default:
    > // do something else
    > }
    > }
    >
    > [snip elaboration]


    Actually this example requires a diagnostic regardless of 6.8.4.2 p2,
    because it has a syntax error. (Only statements, not declarations,
    are allowed after labels, including 'case' labels.)

    However, this got me thinking about the ramifications of how variably
    modified types are defined. Consider the following:

    void
    foo(){
    int n = bar();
    typedef int VLA[n];
    switch( bas() ){
    case 1: ;
    VLA *p = 0;
    /* ... etc 1 ... */ ;
    break;

    default: /* ... etc 2 ... */ ;
    }
    }

    Question: does this function definition have a constraint violation
    or not? Clearly the type VLA is a variable length array type, and so
    a variably modified type. The type of p is derived from a variably
    modified type, and so is also a variably modified type. The switch()
    statement has a 'default' label in the scope of 'p', but that scope
    does not cover the entire switch() statement. So 6.8.4.2 p2 is
    violated, or in other words a constraint violation and a mandatory
    diagnostic, yes? Does the Standard mean to disallow this function
    definition? Or have I misunderstood something somewhere?
    Tim Rentsch, Dec 27, 2012
    #5
  6. Zhang Yuan

    Shao Miller Guest

    On 12/27/2012 10:35, Tim Rentsch wrote:
    > Bart van Ingen Schenau <> writes:
    >
    >> On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:
    >>
    >>> When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
    >>> below ,why we make the switch statement within the scope of that
    >>> variably modified type identifier. Thanks in advance if any one can
    >>> offer some code example or explanation.
    >>>
    >>> ------
    >>> If a switch statement has an associated case or default label within
    >>> the scope of an identifier with a variably modified type, the entire
    >>> switch statement shall be within the scope of that identifier.154)
    >>>
    >>> 154) That is, the declaration either precedes the switch statement,
    >>> or it follows the last case or default label associated with the
    >>> switch that is in the block containing the declaration.
    >>>
    >>> ------

    >>
    >> The paragraph makes constructs like this illegal:
    >>
    >> void foo()
    >> {
    >> int n = bar();
    >> switch(baz())
    >> {
    >> case 1:
    >> char s[n]; // Variably modified type
    >> // do something
    >> break;
    >> default:
    >> // do something else
    >> }
    >> }
    >>
    >> [snip elaboration]

    >
    > Actually this example requires a diagnostic regardless of 6.8.4.2 p2,
    > because it has a syntax error. (Only statements, not declarations,
    > are allowed after labels, including 'case' labels.)
    >
    > However, this got me thinking about the ramifications of how variably
    > modified types are defined. Consider the following:
    >
    > void
    > foo(){
    > int n = bar();
    > typedef int VLA[n];
    > switch( bas() ){
    > case 1: ;
    > VLA *p = 0;
    > /* ... etc 1 ... */ ;
    > break;
    >
    > default: /* ... etc 2 ... */ ;
    > }
    > }
    >
    > Question: does this function definition have a constraint violation
    > or not? Clearly the type VLA is a variable length array type, and so
    > a variably modified type. The type of p is derived from a variably
    > modified type, and so is also a variably modified type. The switch()
    > statement has a 'default' label in the scope of 'p', but that scope
    > does not cover the entire switch() statement. So 6.8.4.2 p2 is
    > violated, or in other words a constraint violation and a mandatory
    > diagnostic, yes? Does the Standard mean to disallow this function
    > definition? Or have I misunderstood something somewhere?
    >


    It looks like a constraint violation to me, too.

    Just as an observation: If it wasn't, and if 'default' was reached and
    had the expression 'sizeof *p' somewhere, the value of 'n' at the time
    of the 'typedef' would've effectively been smuggled down without the
    declaration of 'p' having been reached.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Jan 14, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruce Sam
    Replies:
    2
    Views:
    4,122
    Andrew McDonagh
    Jan 10, 2005
  2. Replies:
    21
    Views:
    1,029
    Giannis Papadopoulos
    Aug 2, 2005
  3. bthumber
    Replies:
    5
    Views:
    406
    Alexey Smirnov
    Jan 29, 2009
  4. Naji

    date modified if statement

    Naji, Oct 25, 2005, in forum: Perl Misc
    Replies:
    30
    Views:
    341
    Joe Smith
    Oct 31, 2005
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    95
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page