Const and array declaration

Source: Internet
Author: User

In stringprep. h of libidn, there is such a statement:

Extern idn_dll_var const stringprep_profiles []; <br/> extern idn_dll_var const stringprep_table_element stringprep_rfc3454_a_1 []; 

The Declaration is implemented in the C file, such as profiles. c

Stringprep_profiles [] ={ <br/> {"nameprep", stringprep_nameprep}, <br/> {"krbprep", stringprep_incluos5},/* deprecate? */<Br/> {"nodeprep", stringprep_xmpp_nodeprep}, <br/> {"resourceprep", stringprep_xmpp_resourceprep}, <br/> {"plain", stringprep_plain }, /* sasl-anon-00. */<br/> {"trace", stringprep_trace},/* sasl-anon-01. */<br/> {"saslprep", stringprep_saslprep}, <br/> {"iscsiprep", stringprep_iscsi},/* obsolete. */<br/> {"iSCSI", stringprep_iscsi},/* IANA. */<br/> {null, null} <br/> }; 

Libidn can be compiled in vs. I have a C ++ project that uses libidn, but an error is reported during compilation,

Error c21_: 'stringprep _ profiles': Unknown size

In msdn, the description of error c2133 is: 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 c2wing:

// C2133.cpp// compile with: /Zastruct X{   int a[0];   // C2133, unsized array};int main(){}

If you change the array declaration of stringprep. h:

Extern idn_dll_var const stringprep_profiles * stringprep_profiles;

Extern idn_dll_var const stringprep_table_element * stringprep_rfc3454_a_1;

It can be edited, but it is incorrect at runtime. stringprep_profiles points to 0x00000003.

Why? It is not necessary to specify the size of all arrays.

Let's take a look at a simplified example. The following example can be edited and run normally.

// F:/My Documents ents/Visual Studio 2008/projects/testproject/arraystruct/arraydefine. h <br/> # pragma once <br/> # ifdef _ cplusplus <br/> extern "C" <br/>{< br/> # endif <br/> typedef struct {<br/> int I; <br/> Int J; <br/>} A; <br/> // const a test []; <br/> A test []; <br/> # ifdef _ cplusplus <br/>}< br/> # endif 

// F:/My Documents ents/Visual Studio 2008/projects/testproject/arraystruct/arraydef. c <br/> # include "arraydefine. H "<br/> // const a test [] = <br/> A test [] = <br/>{< br/> 1, 2 <br /> }, <br/>{< br/> 3, 4 <br/>}< br/> }; 

// F:/My Documents ents/Visual Studio 2008/projects/testproject/arraystruct. CPP <br/> # include "stdafx. H "<br/> # include" arraydefine. H "<br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> printf (" % d, % d/N ", test [0]. i, test [0]. j); <br/> return 0; <br/>} 

If a const is added before the array, such as the commented-out statement, the compilation will fail.

If you add const and change the file name of arraystruct. cpp to arraystruct. C, you can edit it and run it normally.

Through the above example, we can see that C ++ and C are different in terms of const and array declaration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.