我看到这个问题被问了很多次,但是由于某种原因,当我试图在stddef.h头文件中使用offset时,它不会在我的eclipse IDE中解决这个宏。相反,我试图通过手动计算来访问结构。该结构体仅由指针组成。
这是我目前拥有的代码。
int chkpinmode(int header, int pin)
{
/*
* WARNING: This piece of code has no error checking yet!
*/
//Calculations...
unsigned int pinoffset=0;
if(header==8)
{//byte offset
pinoffset=((pin-2)*4);
}
else if(header==9)
{//byte offset
pinoffset=( (44 * 4)+( pin - 11 ) * 4 );//Defines at what point to look at the structure
}
char * pinConf=(char *)(&pins+pinoffset);//No need to worry about padding because structure is made up of pointers
int pinConfValue=-10;
pinConfValue=(int)pinConf;
int mode;
mode=(int)(pinConfValue&7);//Checks one the first 3 bits
return mode;
}
结构体是一个包含volatile int *和一个volatile void *的长列表,它由另一个函数给出地址。头文件中有
extern pins;
是结构。因此,到调用chkpinmode函数时,所有地址都设置好了。
当使用offsetof()编译代码时,它将工作。只是eclipse IDE一直把它标记为错误,但它会工作的。