在libidn的stringprep.h里面,有这样的声明:
声明的实现在c文件里面,例如profiles.c
libidn是可以在vs里面编译通过的,我有一个c++项目要用libidn,但在编译时会报错,
error C2133: 'stringprep_profiles' : unknown size
在MSDN里面,对error C2133的说明为:http://msdn.microsoft.com/en-us/library/c13wk277(VS.71).aspx
| An unsized array is declared as a member of a class, structure, union, or enumeration. The /Za (ANSI C) option does not allow unsized member arrays. The following sample generates C2133: // C2133.cpp
// compile with: /Za
struct X
{
int a[0]; // C2133, unsized array
};
int main()
{
} |
如果你将stringprep.h的数组声明改为:
extern IDN_DLL_VAR const Stringprep_profiles* stringprep_profiles;
extern IDN_DLL_VAR const Stringprep_table_element* stringprep_rfc3454_A_1;
可以编过,但运行时是错误的,stringprep_profiles指向的0x00000003。
这是为什么呢?不至于要将所有的数组都指定大小吧。
先看一个简化的例子,下面的例子是可以编过的,并且运行正常
如果在数组前面加一个const,如注释掉的语句,这时就会编译不过去。
如果你加了const并且将ArrayStruct.cpp的文件名改为ArrayStruct.c也可以编过去并正常运行。
通过上面的例子可以看出c++和c里面对const和数组声明的不同处理了。

本文探讨了解决C++项目中使用未指定大小数组导致的编译错误C2133问题,通过示例展示了如何正确声明和定义数组,以及C与C++在处理此类问题上的差异。

808

被折叠的 条评论
为什么被折叠?



