1,105,352 Community Members

STUCK with STRUCT

Member Avatar
marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
:)

Member Avatar
Moschops
Nearly a Posting Virtuoso
1,373 posts since Sep 2008
Reputation Points: 414 [?]
Q&As Helped to Solve: 266 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

Didn't this already get discussed at length?

http://www.daniweb.com/software-development/cpp/threads/384193

Member Avatar
marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

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

Member Avatar
marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: