Hi,
I write a simple program to understand structure padding.
The code is
Code:
#include <stdio.h>
# include <string.h>
int main()
{
struct test
{
char char1[4];
char char2[2];
}n = {"AB","DCE"};
printf("%s",n.char2);
return 0;
}
I run this program, and i expect the out will come DCE due to structure padding.
But this program give unexpected output "DC@".
This may due to garbage value @ is coming. but i run the same program in different computer and i get the same output.
Again, i modify my program like,
Code:
#include <stdio.h>
# include <string.h>
int main()
{
struct test
{
char char1[4];
char char2[2];
}n;
strcpy(n.char1,"AB");
strcpy(n.char2,"DEF");
printf("%s",n.char2);
return 0;
}
Here i get the output "DEF" as expected.
So, what the difference between above two program.
Sorry for my English,
Thanks