By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

pointer subtraction

P: n/a
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 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 8, 1:11 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
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?

Aug 8 '07 #2

P: n/a
ju**********@yahoo.co.in 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

Aug 8 '07 #3

P: n/a
On Tue, 07 Aug 2007 23:11:32 -0700, ju**********@yahoo.co.in 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 (?)

Aug 8 '07 #4

P: n/a
On Wed, 08 Aug 2007 12:26:39 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
>On Tue, 07 Aug 2007 23:11:32 -0700, ju**********@yahoo.co.in 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
Aug 9 '07 #5

P: n/a
On Wed, 08 Aug 2007 17:57:58 -0700, Barry Schwarz <sc******@doezl.net>
wrote:
On Wed, 08 Aug 2007 12:26:39 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
On Tue, 07 Aug 2007 23:11:32 -0700, ju**********@yahoo.co.in 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
Aug 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.