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
2011-2-24
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Troubleshoot Compilation Errors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!