Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird C program

Check the following code snippet

struct st
{
    struct st
    {
        int a ;
        int b ;
    } st;

    int a1 ;
} ;

struct st obj ;
struct st obj1 ;

int main()
{
    return obj.a1 + obj1.b ;
}

Microsoft's compiler Visual Studio 6.0 compiles the program succesfully. I am confused with the use of 'struct st'. What is the size of obj and obj1?

like image 330
Ganesh Gopalasubramanian Avatar asked Nov 12 '09 18:11

Ganesh Gopalasubramanian


1 Answers

GCC gives

error: nested redefinition of ‘struct st’
error: ‘struct st’ has no member named ‘a1’

If VC6 compiles this, that's fine, but this is invalid.

If you want to know the size of obj, that's sizeof obj. I'd assume VC6 just flattened out the structure and assigned it three ints.

like image 142
Josh Lee Avatar answered Sep 21 '22 07:09

Josh Lee