简体   繁体   中英

C++ 2d array initialization

The following line doesn't work:

int n1=10,v1=10;  
int f[n1][v1]={};
error: variable-sized object ‘f’ may not be initialized

But the line below works, why?

const int n1=10,v1=10;  
int f[n1][v1]={};

Array initializers need to be const.

An int value can change where as a const int value will remain constant throughout the entire program.

In the second example, n1 and v1 are known to be compile-time constants. In the first example, they may not be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM