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

3 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
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.

更多回答(0 个)

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by