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.

Attachments
#include<stdio.h>
#define PI 3.14

struct square
{
    int side;
};

struct circle
{
    int radius;
};

struct rectangle
{
    int side1;
    int side2;
};

union shape_union
{
    struct rectangle rect;
    struct square squa;
    struct circle circ;
};

enum shape_type { RECTANGLE=0, SQUARE, CIRCLE };

struct shape_struct
{
    union shape_union shape_u;
    enum shape_type st;
};

typedef struct shape_struct shape;

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

int main()
{

        int a ;

        shape s_rec = {.shape_u.rect.side1 = 20,.shape_u.rect.side2 = 20, .st = RECTANGLE};
        a = area(&s_rec);
        printf("The area of Rectangle is %d\n", a );

        shape s_cir = {.shape_u.circ.radius = 20, .st = CIRCLE};
        a = area(&s_cir);
        printf("The area of circle is %d\n", a );

        shape s_sq = {.shape_u.squa.side = 20, .st = SQUARE };
        a = area(&s_sq);
        printf("The area of sqaure is %d\n", a );

        return 0;
}

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;
    }
}

Edited 6 Years Ago by Ancient Dragon: n/a

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

Comments
Thanks :)
This article has been dead for over six months. Start a new discussion instead.