We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,047 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

STUCK with STRUCT

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

4
Contributors
5
Replies
6 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

Didn't this already get discussed at length?

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

Moschops
Practically a Posting Shark
888 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

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

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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

mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36

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

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1071 seconds using 2.66MB