Why do I have an error when sizeof is used in a preprocessor directive?

1 view (last 30 days)
the sizeof() function cannot be used in a preprocessor directive, according to the Ansi C standard. But some compilers accept this syntax.
We see here how to bypass the problem.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Feb 2011
For the moment, the only solution is to add some code to bypass the #if sizeof(...) line.
So, if the is something like:
#if (sizeof(myttype) != 8)
// Part 1
// some code
#else
// Part 2
// some code again
#endif
then the user should first evaluate if the condition is true or not (so here if sizeof(mtype) is different from 8 ), and add only the part of code that is true.
So if we consider that sizeof(mtype) is different from 8 (Part 1 is true), the code can be changed to:
#ifndef POLYSPACE
#if (sizeof(mytype) != 8)
// Part 1
// some code
#else
// Part 2
// some code
#endif
#else // POLYSPACE
// Part 1
// some code
#endif
Of course, the flag POLYSPACE should be added to the list of compilation flags (-D) of the project. Hence, the #if (sizeof(mytype) != 8) / #else / #endif part will be ignored (since POLYSPACE is defined) and the part 1 will be the one considered by the preprocessor.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!