473,387 Members | 1,575 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,387 software developers and data experts.

Printing offset of a member of structure

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?

If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.
Thanx in advance for any help ....

Nov 14 '05 #1
10 3980
ju**********@yahoo.co.in writes:
I am trying to print the offset of a particulat member in
a structure, but it's not working.
Use the offsetof() macro, defined in <stddef.h>. That's what it's
for.
I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?
The -> operator binds more tightly than a cast, so you're trying to
cast the result of 0->member_name.
If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.


One possible definition of offsetof() is:

#define offsetof(s, m) (size_t)(&(((s *)0)->m))

(s *)0 is a pointer to a struct at address zero.

((s *)0)->m is a member of that structure.

If the member is at offset 6 within the structure, that member of
the structure at address 0 will have an address of 6.

Converting that address to size_t yields the value 6, which is what
you want.

*All* of this is extremely system-specific. It depends on numerous
assumptions about how address are represented, conversions from
addresses to size_t, and so forth. You cannot make assumptions in
portable code -- but the implementer of the runtime library is
entitled to make whatever assumptions are supported by the compiler.

That's why the offsetof() macro is defined in the standard library;
there's no portable way to implement it, but there's always a
non-portable way that works for a given implementation. The implementer
is allowed to do things that you aren't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2
ju**********@yahoo.co.in wrote:
I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?


It doesn't use the macro the Standard provides for this very purpose,
that's what's wrong with it. Also, you have failed to read the FAQ.
Again.

Richard
Nov 14 '05 #3
ju**********@yahoo.co.in wrote:

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?


It has nothing to do with reality. However there is a standard
macro, named offsetof, to satisfy your urge.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Nov 14 '05 #4
This was what I did to get the offset:
#define OFF(M,S) ((size_t)&(((M *)0)->S))

typedef struct{
int i, j;
char c;
}gen;

I wanted to print offset of c;

size_t size = OFF(gen,c);
printf("%d\n",size);
Try this out.

BTW, I did'nt know there is offsetof which Keith pointed out.

Do tell me if it works

Nov 14 '05 #5
On Mon, 20 Jun 2005 23:33:26 -0700, junky_fellow wrote:
I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name
Standard C providea a macro to do this called offsetof() which is defined
in the standard header <stddef.h>
What's wrong with this ?
-> has higher precedence than a cast so it is parsed as
&(struct mystruct *) (0->member_name)
If I use,
&((struct my_struct *)0)->member_name, it works. But it seems to be
wrong. It looks as if I am trying to print the address of structure
itself instead of the member.


Ys it is generating the address of a structure element for a notional
structure at address 0. You need to convert thjis to an integer to get an
offsdet value. Of course this is non-portable. There is no way to write
portable code to do this directly, so C defines a macro provided by each
implementation which works on that implementations.

Lawrence
Nov 14 '05 #6


Keith Thompson wrote:
ju**********@yahoo.co.in writes:
I am trying to print the offset of a particulat member in
a structure, but it's not working.


Use the offsetof() macro, defined in <stddef.h>. That's what it's
for.
I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?


The -> operator binds more tightly than a cast, so you're trying to
cast the result of 0->member_name.


Thanx a lot for the explanation. Actually, I was confused by the fact
that ()
operator has higher precedence than -> operator.
But, here () have been used as a part of cast operator.

Nov 14 '05 #7
On Tue, 21 Jun 2005 04:26:13 -0700, junky_fellow wrote:

....
Thanx a lot for the explanation. Actually, I was confused by the fact
that ()
operator has higher precedence than -> operator.


The () in precedence tables refers to the function call operator
which has the same precedence as ->. If it was specified with higher
precedence then for example

a.b();

would be parsed incorrectly.

Lawrence
Nov 14 '05 #8
On 21 Jun 2005 02:16:11 -0700, "Rajan" <rs*******@yahoo.com> wrote in
comp.lang.c:
This was what I did to get the offset:
#define OFF(M,S) ((size_t)&(((M *)0)->S))
Dereferencing a null pointer is undefined behavior.
typedef struct{
int i, j;
char c;
}gen;

I wanted to print offset of c;

size_t size = OFF(gen,c);
printf("%d\n",size);
Passing an argument of type size_t to printf() with a "%d" conversion
specifier produces undefined behavior.
Try this out.

BTW, I did'nt know there is offsetof which Keith pointed out.
Have you tried looking it up in your C reference book or copy of the
standard? Or in your compiler's documentation, online help, or man
pages?
Do tell me if it works


Did you type:

offsetof macro

....into the search box at google.com and look at the first few
answers? You don't even need to open the linked articles, several of
them answer your question in the small excerpt on the results page.

Did you read question 2.14 in the FAQ for this newsgroup (link in my
signature)? And its answer, of course.

Note that the offsetof macro produces a value of type size_t, you need
to cast it to int to pass to printf() with a "%d" conversion
specifier.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
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
Nov 14 '05 #9
Jack,
I tried doing this and it gives me absolutely no problems printing the
offset.
Check this out.

Nov 15 '05 #10
"Rajan" <rs*******@yahoo.com> writes:
Jack,
I tried doing this and it gives me absolutely no problems printing the
offset.
Check this out.


You tried what? Check what out? Please provide some context; don't
assume that everyone can easily see the article to which you're
replying.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

In your previous article, you said you used the following to compute
the offset of a struct member:

#define OFF(M,S) ((size_t)&(((M *)0)->S))

As Jack explained, this invokes undefined behavior. Undefined
behavior includes working as you think it should. The OFF macro is
not-portable.

There is a perfectly portable way to compute the offset of a struct
member: the offsetof() macro defined in <stddef.h>.

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>. Question
2.14 talks about the offsetof() macro. It also mentions an alternate
method to be used if the offsetof() macro is not available, but that's
largely obsolete; unless you're using a very old system, the
offsetof() macro *will* be available.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
3
by: Arto Huusko | last post by:
Hello, I'm wondering about the portability and correctness of the following scenario. I have a generic doubly linked list structure, and it contains generic nodes that look like this: ...
15
by: junky_fellow | last post by:
I am trying to find the offset of a member "mbr" inside a structure "str" as follows: offset = &(struct str *)0->mbr; But, on compilation I get the following error: cc: Error: m1.c, line 55:...
5
by: Tom | last post by:
I am converting an old application that was printing directly to a specialized printer device (i.e. a special label printer). It was doing this by opening a file with the file path of 'LPT1:' and...
1
by: mark | last post by:
I can't seem to get the logic for printing multiple pages. I know I have to do a comparison between the bottom margin and the position of the next line to be printed and initiate a has more pages...
10
by: gokrix | last post by:
#include <stdio.h> #include <stdlib.h> struct s { int i; char c; float f; };
7
by: salmonella | last post by:
Hi, I'm trying to print on screen an address of class member function (the function of certain object of course). My code looks like this: class MyClass { public: void fun(void){}; };
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.