something strange about sizeof struct ?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
surya ss
New poster
Posts: 22
Joined: Sat Jun 11, 2005 7:31 pm

something strange about sizeof struct ?

Post by surya ss »

I have a problem with sizeof struct
with this code

Code: Select all

#include <stdio.h>
struct A
{
   int a;
   short b;
};
struct B
{
   int a;
   short b,c;
};
int main()
{
    printf("%d %d\n",sizeof(struct A),sizeof(struct B));
    return 0;
}
how can sizeof( struct A ) and sizeof( struct B ) is 8 ???
can anyone explain ?
and this

Code: Select all

#include <stdio.h>
struct A
{
   long long a;
   short b;
};
struct B
{
   long long a;
   short b,c;
};
int main()
{
    printf("%d %d\n",sizeof(struct A),sizeof(struct B));
    return 0;
}
how can the code above give result 16 for both sizeof(struct A) and sizeof(struct B) ??
can someone please explain ?
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Have you tried using Google? Hint: "sizeof struct".
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...
surya ss
New poster
Posts: 22
Joined: Sat Jun 11, 2005 7:31 pm

Post by surya ss »

Krzysztof Duleba wrote:Have you tried using Google? Hint: "sizeof struct".
well thanks, it doesn't come to my mind
because I just found it to be strange...
Post Reply

Return to “C++”