const与数组声明

来源:互联网 发布:淘宝联系卖家客服 编辑:程序博客网 时间:2024/05/17 04:14

在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: /Zastruct 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和数组声明的不同处理了。

原创粉丝点击