1.11M Members

STUCK with STRUCT

 
0
 

Hi all,
How to find the offset of a structure member just by knowing the structure name and the address of that member?????

For ex.

Struct cook
{

int a;
int b;
char c;
}eat;

void func(int *ptr)
{
//how to find the base address of structure.
//I know only the structure name i.e 'cook' and the address of b i.e 'ptr'

//If the offset to address b is found ,the base address can be found out!!!!!
}

main()
{

func(&eat.b);
getch();

}

Thanks in advance
:)

 
0
 

But still am out of solution!!! The person who asked me this question says there is a solution to this!!!!

 
0
 

offsetof cannot be used in this case because u dont know the member's name but only the address of that member :(

 
0
 

offsetof cannot be used in this case because u dont know the member's name but only the address of that member :(

Whoever gave you this exercise is either mistaken that it's possible, or expecting a non-portable hack. Either way, you need to get clarification, because within standard C there's no solution. You simply cannot find the base address of a structure instance given only a pointer to one of its members and the structure name.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: