473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer subtraction

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
5 1859
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
5
by: Kevin C. | last post by:
udt_list *g_list = calloc(1, LIST_SIZE); udt_entry *entry = get_entry(); // returns a pointer to some address in g_list int numbytes = (char*)entry - (char*)g_list; // supposed to be offset in...
11
by: junky_fellow | last post by:
Can I subtract two pointers of same type that are pointing to the two different location of memory allocated by malloc. eg. #include <stdlib.h> int main(void) { unsigned char *c_ptr;...
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
2
by: James Salisbury | last post by:
Hi, Does anyone have a good page and some exercises on the use of pointers? The compiler is Source Boost and the target is an 18f picr. I am trying to find out why the code below won't compile....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.