structure padding

HI,

I am executing the following code there is no difference,why?

#include <stdio.h>
#include <string.h>

struct structure1
{
int *id2;
char name;

};

struct structure2
{
char name;
int *id2;

};

int main()
{
struct structure1 a;
struct structure2 b;

printf("size of structure1 in bytes : %d\n",
sizeof(a));

printf ( "\n Address of id2 = %u", &a.id2 );
printf ( "\n Address of name = %u", &a.name );

printf(" \n\nsize of structure2 in bytes : %d\n",
sizeof(b));

printf ( "\n Address of name = %u", &b.name );
printf ( "\n Address of id2 = %u", &b.id2 );

getchar();
return 0;
}

O/P:
size of structure1 in bytes : 16

Address of id2 = 2637228064
Address of name = 2637228072

size of structure2 in bytes : 16

Address of name = 2637228048
Address of id2 = 2637228056
Your output is already illustrating the difference:
The first struct has 8 bytes of pointer, 1 byte of char, and 7 bytes of padding.
The second struct has 1 byte of char, 7 bytes of padding, and 8 bytes of int*
Last edited on
but when we use without pointer it is giving difference results, why?
What is "use without pointer"? A structure holding a single char member would have size 1, no padding
Topic archived. No new replies allowed.