C legitimacy?? 
Author Message
 C legitimacy??

Same question I raised in comp.lang.c:

Is it legitmate for compilers to complain at this

int test(char ch)
{
    switch (ch)
    {
    case *"a":
        return 1;
        break;
    }
    return 0;

Quote:
}

The complaint being that *"a" is not a constant expression. Is this ok under
C90 and C99?

Thanks

Stephen Howe
--



Wed, 26 Feb 2003 13:06:05 GMT  
 C legitimacy??

Quote:

> The complaint being that *"a" is not a constant expression. Is this ok
> under
> C90 and C99?

An expression involving a dereference is not required to be a constant
expression; the answer to this question is essentially the same in C as
in C++.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Wretches hang that jurymen may dine.
\__/ Alexander Pope
    Interstelen / http://www.interstelen.com/
 A multiplayer, strategic, turn-based Web game on an interstellar scale.
--



Thu, 27 Feb 2003 00:50:40 GMT  
 C legitimacy??

Quote:

> Same question I raised in comp.lang.c:

Why didn't you crosspost?

Quote:
> Is it legitmate for compilers to complain at this

> int test(char ch)
> {
>     switch (ch)
>     {
>     case *"a":
>         return 1;
>         break;
>     }
>     return 0;
> }

> The complaint being that *"a" is not a constant expression. Is
> this ok under C90 and C99?

At least from C99 6.6 (6) :

# An integer constant expression shall have integer type and shall
# only have operands that are integer constants, enumeration
# constants, character constants, sizeof expressions whose results
# are integer constants, and floating constants that are the
# immediate operands of casts. Cast operators in an integer
# constant expression shall only convert arithmetic types to
# integer types, except as part of an operand to the sizeof
# operator.

So, `*"a"' is not a constant expression.
--
Nils Goesche
Ask not for whom the <CONTROL-G> tolls.
--



Thu, 27 Feb 2003 00:50:48 GMT  
 C legitimacy??
On 09 Sep 2000 05:06:05 GMT, "Stephen Howe"

Quote:

>Same question I raised in comp.lang.c:

>Is it legitmate for compilers to complain at this

>int test(char ch)
>{
>    switch (ch)
>    {
>    case *"a":
>        return 1;
>        break;
>    }
>    return 0;
>}

>The complaint being that *"a" is not a constant expression. Is this ok under
>C90 and C99?

Strings have type "array-of-char", not "array-of-const-char", so *"a" is
indeed not a constant expression. If you want a character label in a
switch, you'll have to do it like

    switch (ch)
    {
    case 'a':
        ...
    }

--
Hong Ooi                    | Centre for Maths and its Applications/

Ph: (02) 6267 4140          | Australian National University
                            | ACT 0200 Australia
--



Thu, 27 Feb 2003 00:50:56 GMT  
 C legitimacy??

Quote:

>     case *"a":

In C99, a case constant is an integer constant expression.  An
integer constant expression may not contain a string literal (see
section 6.6).  I'm sure there's similar language in C89, too.
--



Thu, 27 Feb 2003 00:51:12 GMT  
 C legitimacy??


Quote:
>Same question I raised in comp.lang.c:

Same answer, *"a" is not a constant integer expression.

Quote:

>Is it legitmate for compilers to complain at this

>int test(char ch)
>{
>    switch (ch)
>    {
>    case *"a":
>        return 1;
>        break;
>    }
>    return 0;
>}

>The complaint being that *"a" is not a constant expression. Is this ok
>under C90 and C99?

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Thu, 27 Feb 2003 00:51:36 GMT  
 C legitimacy??

Quote:

> Same question I raised in comp.lang.c:

> Is it legitmate for compilers to complain at this

> int test(char ch)
> {
>     switch (ch)
>     {
>     case *"a":
>         return 1;
>         break;
>     }
>     return 0;
> }

> The complaint being that *"a" is not a constant expression. Is this ok under
> C90 and C99?

> Thanks

AFAIK, yes.  Although a smart compiler could resonably infer that
*"a" == 'a', there's no requirement for it to do so.

There are many expressions which are 'constant', in that a compiler
could, given enough intellegence, infer the constant value of the
expression.  But the C spec is fairly conservative in what it
requires them to be able to infer.

(BTW, return 1; break; is reduntant.  The break is unreachable.)

/peter
--



Thu, 27 Feb 2003 00:51:42 GMT  
 C legitimacy??


Quote:
>> The complaint being that *"a" is not a constant expression. Is this ok
>> under
>> C90 and C99?

>An expression involving a dereference is not required to be a constant
>expression; the answer to this question is essentially the same in C as
>in C++.

You missed the point, in the context of using it as a case for a switch
statement the whole expression does have to be a constant integer
expression.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Fri, 28 Feb 2003 22:07:09 GMT  
 C legitimacy??
Would not then,

int test( char ch )
{
    switch( ch )
    {
       case (const) *"x":
            blah blah blah
            break;
    }

Quote:
}

work under your statement?

I'm pretty sure that any use of *"x", no matter how you format it, does not
match the compiler's interepretation of a constant. The string, "x" is assigned
an address for storage and something like the following bad programming practice
could happen:

    char *ptr = "X";
    *ptr = "C";

Note that *"X" could have been re-written as *ptr above ( assuming the
decleration was made ). Hence, a pointer dereferance cannot be a constant
expression.

Quote:

> On 09 Sep 2000 05:06:05 GMT, "Stephen Howe"

> >Same question I raised in comp.lang.c:

> >Is it legitmate for compilers to complain at this

> >int test(char ch)
> >{
> >    switch (ch)
> >    {
> >    case *"a":
> >        return 1;
> >        break;
> >    }
> >    return 0;
> >}

> >The complaint being that *"a" is not a constant expression. Is this ok under
> >C90 and C99?

> Strings have type "array-of-char", not "array-of-const-char", so *"a" is
> indeed not a constant expression. If you want a character label in a
> switch, you'll have to do it like

>     switch (ch)
>     {
>     case 'a':
>         ...
>     }

> --
> Hong Ooi                    | Centre for Maths and its Applications/

> Ph: (02) 6267 4140          | Australian National University
>                             | ACT 0200 Australia
> --


--



Fri, 28 Feb 2003 22:08:09 GMT  
 C legitimacy??

Quote:

> >     case *"a":

> In C99, a case constant is an integer constant expression.  An
> integer constant expression may not contain a string literal (see
> section 6.6).  I'm sure there's similar language in C89, too.

Huh?  I thought expressions such as:

  sizeof "Foo"

were allowed as integer constant expressions...

--
poncho
--



Fri, 28 Feb 2003 22:09:04 GMT  
 C legitimacy??


Quote:
>I'm pretty sure that any use of *"x", no matter how you format it, does
>not match the compiler's interepretation of a constant. The string, "x"
>is assigned an address for storage and something like the following bad
>programming practice could happen:

>    char *ptr = "X";
>    *ptr = "C";

That is more than bad practice, it invokes undefined behaviour.

Quote:
>Note that *"X" could have been re-written as *ptr above ( assuming the
>decleration was made ). Hence, a pointer dereferance cannot be a
>constant expression.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sun, 02 Mar 2003 03:37:51 GMT  
 C legitimacy??

Quote:

> Erik Max Francis writes:

> > An expression involving a dereference is not required to be a
> > constant
> > expression; the answer to this question is essentially the same in C
> > as
> > in C++.

> You missed the point, in the context of using it as a case for a
> switch
> statement the whole expression does have to be a constant integer
> expression.

Um, so aren't you agreeing with me?

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Many things are lost for want of asking.
\__/ (an English proverb)
    Esperanto reference / http://mirror/alcyone/max/lang/esperanto/
 An Esperanto reference for English speakers.
--



Sun, 02 Mar 2003 03:39:15 GMT  
 C legitimacy??

Quote:




> > >     case *"a":

> > In C99, a case constant is an integer constant expression.  An
> > integer constant expression may not contain a string literal (see
> > section 6.6).  I'm sure there's similar language in C89, too.

> Huh?  I thought expressions such as:

>   sizeof "Foo"

> were allowed as integer constant expressions...

Yes, that's an exception.
--



Sun, 02 Mar 2003 03:39:18 GMT  
 C legitimacy??
Quote:
>On 09 Sep 2000 05:06:05 GMT, "Stephen Howe"


a "case" statement]


Quote:

>Strings have type "array-of-char", not "array-of-const-char" ...

This is true but irrelevant: in C (but not in C++), "const" does
not mean "constant".  (It really means "read-only", and even at
that, it is sometimes more a suggestion than a requirement; but
that is another matter entirely.)  Thus, even if you had:

        const char xyz[] = "xyz";

at (say) file scope, the question as to whether xyz[0], xyz[1],
xyz[2], and xyz[3] were "integral constant expressions" would remain
separate (and the answer would remain "no").

Quote:
>... so *"a" is indeed not a constant expression.

This also is correct, just not tied to the type of string literals.
The things that are "constant expressions" are explicitly spelled
out in the C89 and C99 standards.  Everything not listed is not a
constant expression, and unary "*" is not listed.  (Well, when you
get to the "address constant as initializer", you will find that
unary * can appear in some initializers, as long as it is "cancelled
out" by unary &, as it were.)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Sun, 02 Mar 2003 03:41:03 GMT  
 C legitimacy??

Quote:

> Corin Talkobt writes:

> >    char *ptr = "X";
> >    *ptr = "C";

> That is more than bad practice, it invokes undefined behaviour.

In more than one way, no less!

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ I wouldn't call -- a crazy thing to do / Just in case she said who
\__/ The American, _Chess_
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.
--



Sun, 02 Mar 2003 22:13:35 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. How to show/call Form2.cs from Form1.cs ?

3. Include code in other Cs files

4. Reuse of cs files, namespace, arch advice pls

5. word - automatic numbering/bold/underline/italics

6. How to Generate .cs file at Runtime

7. newbe/cs student, need help w/ code

8. Serial.cs

9. Compile CS source code using ICodeCompiler

10. Two CS files (using namespaces)

11. My .cs files were deleted!?

12. Unable to install Duwamish 7 CS

 

 
Powered by phpBB® Forum Software