Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   pointer to structure from pointer to member (http://www.velocityreviews.com/forums/t444130-pointer-to-structure-from-pointer-to-member.html)

sieg1974@yahoo.com 08-26-2006 01:52 AM

pointer to structure from pointer to member
 
Hi,

Given a pointer to a member within a struct, is it possible to
determine the pointer to the structure? If so, how can it be done?

Thanks,

Andre

struct Structure
{
....
int Integer;
....
};

struct Structure * get_pStructure( int *pInteger )
{
....
}


Frederick gotham 08-26-2006 02:03 AM

Re: pointer to structure from pointer to member
 
sieg1974 posted:


Maybe something like:

#include <stddef.h>

> struct Structure
> {
> ...
> int Integer;
> ...
> };
>
> struct Structure * get_pStructure( int *pInteger )
> {



return (struct Structure*)
((char*)pInteger - offsetof(Structure,Integer));


> }





--

Frederick Gotham

Frederick Gotham 08-26-2006 02:04 AM

Re: pointer to structure from pointer to member
 

> return (struct Structure*)
> ((char*)pInteger - offsetof(Structure,Integer));



Correction:

offsetof(struct Structure,Integer)

--

Frederick Gotham

shaanxxx 08-26-2006 10:10 AM

Re: pointer to structure from pointer to member
 

Frederick Gotham wrote:

> > return (struct Structure*)
> > ((char*)pInteger - offsetof(Structure,Integer));

>
>
> Correction:
>
> offsetof(struct Structure,Integer)
>
> --
>
> Frederick Gotham


#include<stdio.h>

#define offsetof(a,b) (&((a *)0 )->b)

typedef struct xxx{
int a;
int b;
int c ;

}ttt;

int main()
{
printf("%d\n", offsetof(struct xxx,a));
printf("%d\n", offsetof(struct xxx,b));
printf("%d\n", offsetof(struct xxx,c));
}


J. J. Farrell 08-26-2006 10:23 AM

Re: pointer to structure from pointer to member
 

shaanxxx wrote:
> Frederick Gotham wrote:
>
> > > return (struct Structure*)
> > > ((char*)pInteger - offsetof(Structure,Integer));

> >
> > Correction:
> >
> > offsetof(struct Structure,Integer)

>
> #include<stdio.h>
>
> #define offsetof(a,b) (&((a *)0 )->b)
>
> typedef struct xxx{
> int a;
> int b;
> int c ;
>
> }ttt;
>
> int main()
> {
> printf("%d\n", offsetof(struct xxx,a));
> printf("%d\n", offsetof(struct xxx,b));
> printf("%d\n", offsetof(struct xxx,c));
> }


I don't know what you're trying to say in this message, but this
results in undefined behaviour. offsetof() is a Standard macro defined
in <stddef.h>; there's no reason to define a macro of the same name
other than to introduce bugs or reduce portability.


shaanxxx 08-26-2006 11:00 AM

Re: pointer to structure from pointer to member
 

Actually i checked with man page, i didnt find there so i thought it is
not in standard(wrong thoughts). I AM NOVICE TO "C" langauge.


Jack Klein 08-27-2006 01:20 AM

Re: pointer to structure from pointer to member
 
On 26 Aug 2006 04:00:27 -0700, "shaanxxx" <shaanxxx@yahoo.com> wrote
in comp.lang.c:

>
> Actually i checked with man page, i didnt find there so i thought it is
> not in standard(wrong thoughts). I AM NOVICE TO "C" langauge.


THEN DON'T ANSWER QUESTIONS HERE UNTIL YOU ARE AN EXPERT.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


All times are GMT. The time now is 09:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.