pointer subtraction

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Aug 8, 2007.

  1. Guest

    Guys,

    I want to find out an offset of a member inside a structure.
    Although, there is a macro (offsetof) available to do this. Still, I
    have one question. Can I subtract the address of the member from the
    address of structure to get the offset ? If not, what are the reasons
    for not doing this subtraction ?

    Thanks for any help ...
     
    , Aug 8, 2007
    #1
    1. Advertising

  2. On Aug 8, 1:11 am, ""
    <> wrote:
    > Guys,
    >
    > I want to find out an offset of a member inside a structure.
    > Although, there is a macro (offsetof) available to do this. Still, I
    > have one question. Can I subtract the address of the member from the
    > address of structure to get the offset ? If not, what are the reasons
    > for not doing this subtraction ?
    >
    > Thanks for any help ...


    You acknowledge the existence of offsetof() but then proceed to ask
    for ways to re-implement the exact same behavior... why do you need to
    do this?
     
    Justin Spahr-Summers, Aug 8, 2007
    #2
    1. Advertising

  3. Chris Dollin Guest

    wrote:

    > I want to find out an offset of a member inside a structure.
    > Although, there is a macro (offsetof) available to do this. Still, I
    > have one question. Can I subtract the address of the member from the
    > address of structure to get the offset ?


    Yes.

    > If not, what are the reasons
    > for not doing this subtraction ?


    (a) It's only defined to work if you /have/ an instance of the structure.
    (Otherwise, there's nothing to take the address of.)

    (b) offsetof exists. Someone's done the work for me. Laziness rules!

    --
    Chris "to check one's laziness is aligned" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, Aug 8, 2007
    #3
  4. Army1987 Guest

    On Tue, 07 Aug 2007 23:11:32 -0700, wrote:

    > Guys,
    >
    > I want to find out an offset of a member inside a structure.
    > Although, there is a macro (offsetof) available to do this. Still, I
    > have one question. Can I subtract the address of the member from the
    > address of structure to get the offset ?

    You have to convert them both to unsigned char *.

    > If not, what are the reasons for not doing this subtraction ?

    Because there's no need to reinvent the wheel.
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
     
    Army1987, Aug 8, 2007
    #4
  5. On Wed, 08 Aug 2007 12:26:39 +0200, Army1987 <>
    wrote:

    >On Tue, 07 Aug 2007 23:11:32 -0700, wrote:
    >
    >> Guys,
    >>
    >> I want to find out an offset of a member inside a structure.
    >> Although, there is a macro (offsetof) available to do this. Still, I
    >> have one question. Can I subtract the address of the member from the
    >> address of structure to get the offset ?

    >You have to convert them both to unsigned char *.


    If the only action is to subtract one pointer from another, any of the
    char types will work.

    >
    >> If not, what are the reasons for not doing this subtraction ?

    >Because there's no need to reinvent the wheel.



    Remove del for email
     
    Barry Schwarz, Aug 9, 2007
    #5
  6. On Wed, 08 Aug 2007 17:57:58 -0700, Barry Schwarz <>
    wrote:

    > On Wed, 08 Aug 2007 12:26:39 +0200, Army1987 <>
    > wrote:
    >
    > >On Tue, 07 Aug 2007 23:11:32 -0700, wrote:
    > >
    > >> Guys,
    > >>
    > >> I want to find out an offset of a member inside a structure.
    > >> Although, there is a macro (offsetof) available to do this. Still, I
    > >> have one question. Can I subtract the address of the member from the
    > >> address of structure to get the offset ?


    Other way, obviously: subtract base address from member address.

    > >You have to convert them both to unsigned char *.

    >
    > If the only action is to subtract one pointer from another, any of the
    > char types will work.
    >

    Right.

    > >
    > >> If not, what are the reasons for not doing this subtraction ?

    > >Because there's no need to reinvent the wheel.

    >

    Right.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Aug 26, 2007
    #6
    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. Replies:
    3
    Views:
    16,021
    Eric Smith
    Jan 18, 2006
  2. August1
    Replies:
    4
    Views:
    353
    August1
    Sep 21, 2004
  3. Kevin C.

    Pointer subtraction

    Kevin C., May 7, 2004, in forum: C Programming
    Replies:
    5
    Views:
    430
  4. pointer subtraction and sizeof

    , Jan 12, 2007, in forum: C Programming
    Replies:
    20
    Views:
    709
    Peter Nilsson
    Jan 16, 2007
  5. Replies:
    9
    Views:
    709
    Christopher Bazley
    Jan 30, 2010
Loading...

Share This Page