future
 
 
  Joined: 14 May 2004 Posts: 330
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| The use of 'offsetof' | 
			 
			
				 Posted: Mon Mar 12, 2012 8:51 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Mar 12, 2012 9:09 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 | 
			 
		  |