pointer to structure from pointer to member

Discussion in 'C Programming' started by sieg1974@yahoo.com, Aug 26, 2006.

  1. Guest

    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 )
    {
    ....
    }
     
    , Aug 26, 2006
    #1
    1. Advertising

  2. 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, Aug 26, 2006
    #2
    1. Advertising


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



    Correction:

    offsetof(struct Structure,Integer)

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 26, 2006
    #3
  4. shaanxxx Guest

    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));
    }
     
    shaanxxx, Aug 26, 2006
    #4
  5. 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.
     
    J. J. Farrell, Aug 26, 2006
    #5
  6. shaanxxx Guest

    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.
     
    shaanxxx, Aug 26, 2006
    #6
  7. Jack Klein Guest

    Jack Klein, Aug 27, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,076
    Fraser Ross
    Aug 14, 2004
  2. A
    Replies:
    27
    Views:
    1,659
    Jorgen Grahn
    Apr 17, 2011
  3. Stephen Howe
    Replies:
    2
    Views:
    303
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    300
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    173
    somenath
    Aug 29, 2013
Loading...

Share This Page