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
:)

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.