网络推荐

推荐给好友 上一篇 | 下一篇

const与数组声明

oi6U2G en6g7xN7D在libidn的stringprep.h里面,有这样的声明BSD爱好者乐园Mt!R }"d

BSD爱好者乐园;bRm9EI]a,` a

view plaincopy to clipboardprint?BSD爱好者乐园ja.RIM1P]9E
extern IDN_DLL_VAR const Stringprep_profiles stringprep_profiles[];  BSD爱好者乐园k-]+}G&U:|AnG
extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_A_1[]; 
,K*?(Hjm'Y*@extern IDN_DLL_VAR const Stringprep_profiles stringprep_profiles[];BSD爱好者乐园fss&}1[ET
extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_A_1[];BSD爱好者乐园-_^C'y0PG4t*] j

5}4F-j'f@.\x声明的实现在c文件里面,例如profiles.cBSD爱好者乐园"y*W S)z"V,S&N

~d [8d h7{view plaincopy to clipboardprint?BSD爱好者乐园1|xwJ3s O
Stringprep_profiles stringprep_profiles[] = {   
+l"P~ Y2`$PM  {"Nameprep", stringprep_nameprep},   BSD爱好者乐园 Pa,~/uJ"q)b
  {"KRBprep", stringprep_kerberos5},    /* Deprecate? */   
F X+dq \vxn*W0U  {"Nodeprep", stringprep_xmpp_nodeprep},   
P$g+hD!W  {"Resourceprep", stringprep_xmpp_resourceprep},   
4iK4fV'v/Y8L  {"plain", stringprep_plain},    /* sasl-anon-00. */   BSD爱好者乐园tC!@\u*{}
  {"trace", stringprep_trace},    /* sasl-anon-01,02,03. */   BSD爱好者乐园%Ae,q,`8u G2qU8y
  {"SASLprep", stringprep_saslprep},   BSD爱好者乐园I4OVl P0a5A
  {"ISCSIprep", stringprep_iscsi},    /* Obsolete. */   
9co imX9jz  {"iSCSI", stringprep_iscsi},    /* IANA. */   
R"v([2Sf.m  {NULL, NULL}   BSD爱好者乐园)u&cu J/ww*Z4QL
}; 
^)~-`'ZG)weStringprep_profiles stringprep_profiles[] = {BSD爱好者乐园7n f#n-SOTU4]
  {"Nameprep", stringprep_nameprep},BSD爱好者乐园(M!X;rS4n/K
  {"KRBprep", stringprep_kerberos5},    /* Deprecate? */
/Zkr]1iVE9OR  {"Nodeprep", stringprep_xmpp_nodeprep},BSD爱好者乐园Y9t_]ad
  {"Resourceprep", stringprep_xmpp_resourceprep},
dvodYe  {"plain", stringprep_plain},    /* sasl-anon-00. */BSD爱好者乐园3gl"] TWDBS
  {"trace", stringprep_trace},    /* sasl-anon-01,02,03. */BSD爱好者乐园!f3jDk cdK+n~V
  {"SASLprep", stringprep_saslprep},BSD爱好者乐园'}Z kGfSX A%[0CP
  {"ISCSIprep", stringprep_iscsi},    /* Obsolete. */BSD爱好者乐园-{3Ae^,r?N
  {"iSCSI", stringprep_iscsi},    /* IANA. */
4P?$lp%Fs(s  {NULL, NULL}
B ge.K-ZA v*o9H};BSD爱好者乐园*D1BTD7B&X#D#Hx

BSD爱好者乐园%F9AnrU(i

libidn是可以在vs里面编译通过的,我有一个c++项目要用libidn,但在编译时会报错,

&p&z3Qv)J l

0x0oF#h Werror C2133: 'stringprep_profiles' : unknown size

g1N[gL8W-x ~BSD爱好者乐园L_ n1d:t/YpV

在MSDN里面,对error C2133的说明为:http://msdn.microsoft.com/en-us/library/c13wk277(VS.71).aspx

Wn2bm%tm)V GB z

WSZ9P)J7]B2fAn 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.

P!V)L7bVI(DZx

.~D,x%S-yThe following sample generates C2133:BSD爱好者乐园Xer `s^j

BSD爱好者乐园${G+}.YcMpO9@ QS$v

// C2133.cpp
:IY/F)om4h(Ah// compile with: /Za
(|2n@*^W5e [Q5^struct X
oqLp wq`3r/z{
O{!P:z}   int a[0];   // C2133, unsized array
OdI rIbeV};BSD爱好者乐园IAR A(PM.c

BSD爱好者乐园 o3W5yb(y+hgk,Qb

int main()BSD爱好者乐园*X Ew+^7G`E1v
{
FzkkiB,E5I~}

k;a!f6v!q5fp9| X`BSD爱好者乐园5G,b(]A4W4{y

如果你将stringprep.h的数组声明改为:

S7n/T.pK-MEBSD爱好者乐园4N/o:@d!SGQq(w

extern IDN_DLL_VAR const Stringprep_profiles* stringprep_profiles;

+Q'b5}8cE0p#|z

C;W(E/jV4pWa?[Bextern IDN_DLL_VAR const Stringprep_table_element* stringprep_rfc3454_A_1;

K6S#?-`2c

:nD*] i@+u!{G可以编过,但运行时是错误的,stringprep_profiles指向的0x00000003。

N#z'a-x fBSD爱好者乐园 }m Sj E7~9w!o"LW

这是为什么呢?不至于要将所有的数组都指定大小吧。BSD爱好者乐园|l,@ | iy\ AT'\

BSD爱好者乐园J.HG+~.G9skj

先看一个简化的例子,下面的例子是可以编过的,并且运行正常BSD爱好者乐园8l9JY4To d4N!N

BSD爱好者乐园7q? m)R8a(X)y9i!P(E

view plaincopy to clipboardprint?
eV'ur*U _4Q:PPl\-g2g$Z//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDefine.h  
~mB\ U4o#pragma once  
/rY(Fq F5S:m#ifdef __cplusplus   BSD爱好者乐园9Kf_T3l
extern "C"   BSD爱好者乐园.M7[ T7a2i1rF ul U
{   
L e#d5H;m#endif  BSD爱好者乐园 T|0H$fb1l!|
typedef struct{   
%`*w.t7tzQ     int i;   BSD爱好者乐园\] t/\G]-ed#`
     int j;   
3X0I)x ~3hh} A;  BSD爱好者乐园,MI ^ g W7bE/sh
//const A test[];   BSD爱好者乐园z_~K4I,Xq%D
A test[];  
3hQB3iWHf2n7n#ifdef __cplusplus   BSD爱好者乐园{7y6t6?;xkw{*I)J:F3o:~
}   BSD爱好者乐园] |Vb(T+?
#endif 
!LX$ap+wH-`//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDefine.hBSD爱好者乐园#vx9|-k;A&PAC]:u*O
#pragma once
Qn;?$kUe3@)qMe#ifdef __cplusplus
aL+o#Q xx(Qzextern "C"BSD爱好者乐园^ U/\gg4@s"\2wL
{
7B3S]o7}D#endifBSD爱好者乐园0e~+A(Ao q;R
typedef struct{
1Z5l?@Z~     int i;BSD爱好者乐园$BlIaH#l
     int j;BSD爱好者乐园 a^]0[f(BV^P
} A;
j,P#Z M.[@//const A test[];BSD爱好者乐园~ m l'unN
A test[];BSD爱好者乐园N:zM] v:o)c:vn2i`
#ifdef __cplusplusBSD爱好者乐园8jz.h+z s~yM6v7G
}BSD爱好者乐园9? K2Nm#]4F&D-\ Y&pM
#endif

hgM M`_W

j$i`;]MwV3Sview plaincopy to clipboardprint?
BC d.nJdS//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDef.c  BSD爱好者乐园*hY(a2b)Git(_+Q
#include "ArrayDefine.h"  
w/|@Gl5~ e//const A test[]=   
*MA0X%^/@#v r&\[ VA test[]=   
$E1QpW_&T2W A Y&Q{   
m7hdK2A    {   BSD爱好者乐园&Q"V.jC4]e#S7r
        1,2   
FywKZR*}h^ y x1?m    },   BSD爱好者乐园cEF6mMJ7dEEj
    {   
0j z!M.N8DN_&NM        3,4   
7BD/y/~(j/_t    }   BSD爱好者乐园u;DNyo xGr
}; BSD爱好者乐园g` CU&CT@
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDef.c
#N(D$AX!Yz {F#include "ArrayDefine.h"
!q~(]oJ%h(kux P//const A test[]=
@ba]8X8ZWe'QA test[]=BSD爱好者乐园u9IQ8?tQT
{
3b0f,T9A&y    {
f9Y9n,uU:jS        1,2BSD爱好者乐园BVt2cQ&m!X
    },
Ne.ni0s7]%R    {
'~"C{C6z4Pa        3,4
lGQ y_ Qk N    }
N&\K Hy8M};

3ZB5kT feBSD爱好者乐园o&du;K/h I0@?

view plaincopy to clipboardprint?
7v8N(CmN"^//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayStruct.cpp  BSD爱好者乐园8m^x@-~/agJiM~
#include "stdafx.h"   BSD爱好者乐园];p:T/T:NUY
#include "ArrayDefine.h"  BSD爱好者乐园!w6tnHC1a*DKEB9L
int _tmain(int argc, _TCHAR* argv[])   BSD爱好者乐园H8KL Kos6\9?f
{   
-n!N8R r[    printf("%d,%d\n",test[0].i,test[0].j);   BSD爱好者乐园,G]QgF#R/V.f
    return 0;   
f(t'h&P/Q W,|BSD爱好者乐园9eS)f0X8s
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayStruct.cppBSD爱好者乐园%{2PC d'xE#O ek
#include "stdafx.h"
-q3@[I4X#H%` B#include "ArrayDefine.h"
,BGiy:K%Y{e0fMint _tmain(int argc, _TCHAR* argv[])BSD爱好者乐园&G;{I{$MX2i~P
{
0wiV4a"\%ub2H    printf("%d,%d\n",test[0].i,test[0].j);
D7e qS0E7En}%P    return 0;BSD爱好者乐园9sg1FR!E tUUig
}BSD爱好者乐园y"Z+} cW@0SoA%A

BSD爱好者乐园G@jox'e

如果在数组前面加一个const,如注释掉的语句,这时就会编译不过去。

ra3Z2Z7~BSD爱好者乐园*H!oo/hC$hEI6}

如果你加了const并且将ArrayStruct.cpp的文件名改为ArrayStruct.c也可以编过去并正常运行。

U2`\ IY$E+s] k

S)i-tS8s$U4~\通过上面的例子可以看出c++和c里面对const和数组声明的不同处理了。

@d-ls~ra因为使用范围的限制,BSD方面文章更新速度不快,站长会坚持每天更新博客,欢迎访问!
[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: const 数组 声明
 

评分:0

我来说两句

seccode