0

I have type:

typedef struct 
{
   int x;
   int y;
   int z; 
} sdf_test_t;

But when I try to compile the following:

offset = offsetof(sdf_test_t, z);

Visual Studio responds with:

c:\dataflash.c(542) : error C2143: syntax error : missing ')' before 'type'
c:\dataflash.c(542) : error C2059: syntax error : ')'

What is wrong here?

I am using:

Microsoft Visual Studio 2008 x86 
Microsoft (R) Visual Studio Version 9.0.21022.8.

The offsetof macro is defined in <stddef.h> as follows:

/* Define offsetof macro */
#ifdef __cplusplus

#ifdef  _WIN64
#define offsetof(s,m)   (size_t)( (ptrdiff_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )
#else
#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
#endif

#else

#ifdef  _WIN64
#define offsetof(s,m)   (size_t)( (ptrdiff_t)&(((s *)0)->m) )
#else
#define offsetof(s,m)   (size_t)&(((s *)0)->m)
#endif

#endif  /* __cplusplus */

By elimination. I've established that the compiler uses:

#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
NZD
  • 1,780
  • 2
  • 20
  • 29

2 Answers2

2

I've made a simple program as it:

#include <stddef.h>

typedef struct 
{
  int x;
  int y;
  int z; 
} sdf_test_t;

int main() {
  size_t offset = offsetof(sdf_test_t, z);
  return 0;
}

I don't have any problems, i think that you can try to isolate the code in another project and test it again.

Heros
  • 340
  • 5
  • 15
  • Thanks for that. I found out that the crucial point was I had to include ``, so it will use the `C` variant of the macro and not the default `C++` variant. – NZD Aug 05 '15 at 21:25
0

I managed to fix it by adding the following line to my source file:

#include <stddef.h>

From this, it seems that Visual Studio silently includes header files if you don't include them explicitly. Even worse, It assumes that the source file is C++ by default.

If I don't include a header file with a symbol I use, I would expect the compiler to scream out and report an error, not just make up something...

NZD
  • 1,780
  • 2
  • 20
  • 29