Hi,
I used Enum constant as a case of swict statement and trying to select by using the pointer to the structure , which internally has a union and a enum member.
the union member again internally have three structures.

even i debugged the code.

the value of the variable s -> st is getting the correct value in debug mode but the function is returning -1.

Please have a look into the code in the attachment.

Thanks,
Danian.

Recommended Answers

All 5 Replies

you forgot the case keyword. Your program contains millions of other errors that you need to correct -- if your compiler doesn't report them then toss out that crappy compiler and get one that actually works right.

int area(shape *s)
{
 switch(s->st)
    {
        case RECTANGLE:
            return (s->shape_u.rect.side1) * ( s->shape_u.rect.side2);
        case SQUARE:
            return (s->shape_u.squa.side) * (s->shape_u.squa.side);
        case CIRCLE:
            return (int)(s->shape_u.circ.radius * 2 * PI);
        default:
            return -1;
    }
}

you forgot the case keyword. Your program contains millions of other errors that you need to correct -- if your compiler doesn't report them then toss out that crappy compiler and get one that actually works right.

int area(shape *s)
{
 switch(s->st)
    {
        case RECTANGLE:
            return (s->shape_u.rect.side1) * ( s->shape_u.rect.side2);
        case SQUARE:
            return (s->shape_u.squa.side) * (s->shape_u.squa.side);
        case CIRCLE:
            return (int)(s->shape_u.circ.radius * 2 * PI);
        default:
            return -1;
    }
}

yes sir , you are right.

f your compiler doesn't report them then toss out that crappy compiler and get one that actually works right.

but i am using GCC compiler.
it is not throwing any warnings or error for this.
switch with out case should be a error.

yes sir , you are right.

but i am using GCC compiler.
it is not throwing any warnings or error for this.
switch with out case should be a error.

You are right -- Code::Blocks (which also uses MinGW ) doesn't produce warnings or errors on that either. VC++ 2008 Express produces a warning. I'm not familiar enough with gcc to know if there are any flags that will make that compiler produce a warning on it.

You are right -- Code::Blocks (which also uses MinGW ) doesn't produce warnings or errors on that either. VC++ 2008 Express produces a warning. I'm not familiar enough with gcc to know if there are any flags that will make that compiler produce a warning on it.

i will check out and let you know sir.

You are right -- Code::Blocks (which also uses MinGW ) doesn't produce warnings or errors on that either. VC++ 2008 Express produces a warning. I'm not familiar enough with gcc to know if there are any flags that will make that compiler produce a warning on it.

there are some flags which enables the missing of case or default or both in an out side of switch and when used with enum as case constant.

these are: gcc filename.c -Wswitch-enum warns whenever a switch statement has an index of enumarated type and lacks a "case" for one or more of the named codes of that enumeration

or simply we can use gcc filename.c -Wall

commented: Thanks :) +25
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.