On 15 Feb 2007 18:45:42 -0800, "somenath" <so*********@gmail.com>
wrote:
>Hi all,
Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
here the output of "the modificated version" was something like
-----------------------------------
70
1
1
girono=1
mese =1
anno =2006
-----------------------------------
girono dp1=1
mese dp1=1
anno dp1=2006
because ++dp; jump sizeof(MYDATE)==here to 3 ints
but all this is UB (the output dipends from how compiler dispose the
data)
thats UB so if someone run it he/she shold know all can happen
>typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };
main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70
Regards,
Somenath
#include <stdio.h>
typedef struct
{ int day;
int month;
int year;
} mydate;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
mydate abc = { 1, 1, 2006 };
int main(void)
{mydate *dp = &x, *dp1=&y;
if(sizeof(mydate)!= 3*sizeof(int) || &x+7!=&abc)
{printf("I don't like padding\n");
return 0;
}
++dp; ++dp;
++dp1; ++dp1;
printf("%x\n", dp->day);
printf("%x\n", dp->month);
printf("%x\n", dp->year);
/* here "dp" point to &d */
/* here ((int*)dp + 1) point to &abc */
printf("girono=%d \n", ((mydate *)( (int*)dp + 1))->day);
printf("mese =%d \n", ((mydate *)( (int*)dp + 1))->month);
printf("anno =%d \n", ((mydate *)( (int*)dp + 1))->year);
printf("girono dp1=%d \n", dp1->day);
printf("mese dp1=%d \n", dp1->month);
printf("anno dp1=%d \n", dp1->year);
return 0;
}