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

Recommended Answers

Ever heard of "Google" the mighty search machine?
Let me introduce you to what you can find with it: http://stackoverflow.com/questions/142016/c-c-structure-offset

Jump to Post

All 5 Replies

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.