Hi ppl
I have the following code snippet:
#include "stdafx.h"
#include "conio.h"
#include "malloc.h"
#include <stdio.h>
#include <stddef.h>
struct base
{
char c;
int i;
};
struct der
{
base b;
};
void FillUp(der *p)
{
int temp1=offsetof(struct der,b);
int temp2=offsetof(struct base,i);
//LINE 3
printf("\ntemp1 is:%d",temp1); //gives 0
//LINE 4
printf("\ntemp2 is:%d",temp2); //gives 4
int *ptr;
ptr=(int*)p+temp1/sizeof(base)+temp2/sizeof(int);
printf("\nInside Fillup: 0x%08X",ptr); //gives 0012fed4
*ptr=10;
}
int _tmain(int argc, _TCHAR* argv[])
{
der d;
int temp1=offsetof(struct der,b);
int temp2=offsetof(struct base,i);
int *ptr;
// LINE 1
printf("\ntemp1 is:%d",temp1); //gives 0
//LINE 2
printf("\ntemp2 is:%d",temp2); //gives 4
ptr=(int*)&d+temp1/sizeof(base)+temp2/sizeof(int);
printf("\nInside main: 0x%08X",ptr); //gives 0012fed4
FillUp(&d);
printf("\nnormal way is: 0x%08X",&d.b.i);
printf("\n%d",d.b.i); //gives 10
getch();
return 0;
}
The code snippet is fully working. But I have a small problem when I am
using the above architecture in a bigger project.
LINE 1 gives 0
LINE 2 gives value x
LINE 3 gives 0
LINE 4 gives value y
When can be the case that there is a conflict and line 2 and line 4
gives different values?? How is is possible that the offset that has
been calculated for same member for same structure is different in 2
cases??
Any help would be priceless....I am totally foxed!!!
Thanks very much
regards
HARRY