CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

The use of 'offsetof'

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

The use of 'offsetof'
PostPosted: Mon Mar 12, 2012 8:51 am     Reply with quote

Hello,

I am trying to retrieve the offset of a struct member.

Code:
typedef struct __cfg__ {
    char           params[200];
};

typedef struct __cfg_file__ {
    char           name[17];
    struct __cfg__ table;
};

typedef struct __fram__ {
    struct __cfg_file__   config[3];
};

long offset;
offset = offsetof(struct __fram__, config[2].params[100]);

The compiler does not like the config "2" index. The error message is that a parenthesis is missing there.

What am I doing wrong?

Thank you.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Mar 12, 2012 9:09 am     Reply with quote

From the CCS manual entry for offsetof(), though this is not specific to CCS, its normal C:

Quote:

These functions return an offset into a structure for the indicated field. offsetof returns the offset in bytes and offsetofbit returns the offset in bits.


config is a field of your structure __fram__ and is all offsetof can tell you about. The error message is telling you that a ) was expected after config: i.e. offsetof(struct _fram__, config) is valid syntax.

config[2] is an element of the field config of __fram__.

config[2].params is a field of the structure that is an element of the field config of __fram__.

So by now you should be getting the message that there's no way config[2].params[100] can be a field of __fram__. To get the offset of that in __fram__ you'd have to do two offsetofs and quite a bit of array index manilpulation.

My question, however, is why? What are you trying to acheive by this? Its very likely there is another way of doing what you want. I for one, have never used offsetof, generally being able to use pointers or references instead.

RF Developer
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group