初始化变量长度数组 [英] Initializing variable length array

查看:210
本文介绍了初始化变量长度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在初始化变量长度数组编译器给出错误信息:

On initializing a Variable length array compiler gives an error message:

[Error] variable-sized object may not be initialized  

code片断:

Code snippet:

int n; 
printf("Enter size of magic square: ");
scanf("%d",&n);

int board[n][n] = {0};

应该如何变长数组初始化?
以及它为什么所有的元素都不会初始化为 0 的方式在下面;

How should Variable Length arrays be initialized? And why it's all elements are not initialized to 0 in the way give below;

   int board[n][n];
   board[n][n] = {0};

推荐答案

沃拉斯不能的初始化的任何形式的初始化语法。你要的分配的,无论在哪,你的方法preFER宣布后的初始值到你的数组元素。

VLAs cannot be initialized by any form of initialization syntax. You have to assign the initial values to your array elements after the declaration in whichever way you prefer.

没有初始化应尝试提供不包含在实体被初始化的对象的值。

No initializer shall attempt to provide a value for an object not contained within the entity being initialized.

实体来进行初始化应是未知大小或一个完整的对象类型的数组类型不是一个可变长度的数组类型

The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

这篇关于初始化变量长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆