Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Strut pointer

Posted on 2007-11-27
7
Medium Priority
?
421 Views
Last Modified: 2010-04-21
Given a poniter to a member "m" within a struct, write a function that returns the pointer to the struct.
    struct s {
       .....
       int m;
       .....
    };

  struct s * get_struct_pointer(int * ptr);      
0
Comment
Question by:qiang_lin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359496
Homework?
Some steps, you have to know the "distance" between some struct member and the FIRST member in the same structure
Once you know the distance (some few bytes, usually), sustract this distance from the pointer.
to do calculation, all your pointers should be casted to (char *) to operate in bytes, if not you will have strange results.
0
 
LVL 22

Accepted Solution

by:
grg99 earned 375 total points
ID: 20359580
Hint:   The  & operator can be used to take the address of structs or fields in a struct.


And if you store addresses in a char *,  then subtract those, you can get the distance between.

Similarly you can take the address of a field and subtract from it to get ....


BTW this is a very very dirty thing to do.  You should never work backwards from fields to the (supposedly) encompassing structure.

0
 
LVL 10

Expert Comment

by:peetm
ID: 20359844
You presumably either have the member 'm' as the first member of the struct, OR, you know the types of the members before 'm'?

My gut feel is that this is a pretty horrible thing to ever do or attempt - lock down the struct definition for sure!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:evilrix
ID: 20360292
In theory, if m is the first element in s then m and s should have the same address, if not you have no way to calculate this without knowing that comes before it. Even then you have no guarantees of this as it all depends upon the packing and alignment used by the compiler!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20360444
Here is a macro defined in winnt.h that does the job for an arbitrary struct:

#define CONTAINING_RECORD(address, type, field) ((type *)( \
                                                  (PCHAR)(address) - \
                                                  (UINT_PTR)(&((type *)0)->field)))

You would call it like

 struct s *  ps = CONTAINING_RECORD(pm, struct s, m);

It was casting a NULL pointer to struct s pointer and retrieves the 'address' of member m of that NULL pointer, so getting the offset in bytes. Then it makes a cast to a  unsigned int* (what I don't understand) and subtracts that address (what actually is the offset of m within s) from the given pointer casted to a PCHAR what is a char pointer.


Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20360494
Note, the above was a macro. So it can use the fieldname 'm' to access member m within struct s and can use the typename 'struct s' passed as argument  to make casts. If your task is to make it a C function you have to 'know' that it is 'struct s' and that the address passed is that of member 'm'.
0
 

Author Closing Comment

by:qiang_lin
ID: 31411234
The hint is very good. Actually we can creat a "struct s" object and find out the distance between this object and its member m. The distance is consistant among all "struct s" objects.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question