Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-29-2011, 03:11 AM   #1
LQ Newbie
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
C typecasting offset

Hello everyone!!
I am new to Linux forum and have a question related to typecasting in C related to offset of structures.I have a piece of code and having a problem with this:
void fun(struct c *p)
int offset;
struct b *address;
offset=(char *)&((struct c *)(&((struct a*)0)->y)->j)-(char *)((struct a *)0);
address=(struct b *)((char*)&(p->j)-offset);
As you can see that this code is accessible in turbo C but not in Dev C++ and please anybody tell me why is this so and any method to easily typecast structures for finding their offset.
Old 12-29-2011, 06:06 AM   #2
Senior Member
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 343Reputation: 343Reputation: 343Reputation: 343
Hi there,

I haven't yet understood what you really want to do. Compute the byte offset of a structure member relative to the base address of that structure? So, for this arbitrary sample structure
typedef struct
 { WORD  w;
   BYTE  b;
   DWORD d;
 } FOO;
you want to get 3 for d?
Then all you need is
FOO *foo = NULL;
int  offset = (int) &(foo->d);
But your sophisticated sample, that I didn't dig into yet, looks like you want something else.

offset=(char *)&((struct c *)(&((struct a*)0)->y)->j)-(char *)((struct a *)0);
address=(struct b *)((char*)&(p->j)-offset);
Isn't there a more complicated way to write that? ;-)
Besides, for a better understanding you should have shown us the definition of struct a, struct b and struct c.

[X] Doc CPU


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bitfields query.. after typecasting. Santoshkb Programming 2 01-15-2012 01:55 AM
typecasting z99 Programming 4 07-03-2011 12:54 PM
Typecasting void pointer saurabhchokshi Programming 5 04-14-2010 02:00 PM
my monitor is offset kenan6346 Linux - Newbie 8 05-05-2008 04:34 AM
Download-Offset possible? Cornholio Linux - Software 0 01-10-2005 04:47 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration