Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#95 label minus label is not constant

open
nobody
None
5
2009-09-23
2005-01-18
Josef Pavlik
No

hi all,
I found, that is impossible to compile the following piece of code. The error is, that the Initializer element is not constant.

struct test
{
int a;
int b;
} nv;

const code int x=(char*)&nv.b-(char*)&nv;

Discussion

  • Maarten Brock
    Maarten Brock
    2006-02-13

    Logged In: YES
    user_id=888171

    Josef,

    You are correct that this produces an error. But for cases
    like this offsetof() is defined in stddef.h.

    #include <stddef.h>
    const int x = offsetof (struct test, b);

    I'm not sure if we need to treat this as a bug or a feature
    request. I tend towards the latter.

    Maarten

     
  • It seems to me that this is a feature request. From the C99 standard:

    "An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants,
    and floating constants that are the immediate operands of casts."

     
    • labels: 101552 -->