-1

I have seen this question asked various times but for some reason when I attempt to use offsetof with the stddef.h header it will not resolve this macro in my eclipse IDE. Instead I am trying to access the structure by calculating it manually. This structure consists of only pointers.

Here is the code I currently have.

 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;
    }

The structure is a long list of volatile int * with one volatile void * and it given addresses by another function. The header file has

extern pins;

which is the structure. So by the time function chkpinmode is called all the addresses are set.

cmacia06
  • 189
  • 1
  • 1
  • 13
  • 1
    "Eclipse doesn't resolve the macro" is not a good reason not to use it. – Eugene Sh. May 15 '15 at 17:23
  • Thank you! That is why... Now my next question is if I use strcat to create the text of a item in the structure, can u use that string as the input to offsetof? – cmacia06 May 15 '15 at 17:34
  • 1
    I don't understand how you're intending to use offsetof. It's usually just a compile-time calculation of a relative address within a structure. You won't be able to use a string variable as one of its args. What exactly do you mean by eclipse won't 'resolve this macro'? – Spalteer May 15 '15 at 18:02
  • The code posted does not use `offsetof`. And don't use `strcat` to "create" a string, you use `strcpy` or `sprintf`, but `strcat` appends to an already existing (possibly empty) valid string. But your code does not show how you use `strcat` either. – Weather Vane May 15 '15 at 18:27

1 Answers1

0

When compiling code using offsetof(), it will work. It is just the eclipse IDE that kept marking it as an error but it will work.

cmacia06
  • 189
  • 1
  • 1
  • 13