C 编译错误:“可能无法初始化可变大小的对象" [英] C compile error: "Variable-sized object may not be initialized"

查看:31
本文介绍了C 编译错误:“可能无法初始化可变大小的对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我收到以下代码的错误可变大小的对象可能无法初始化"?

Why do I receive the error "Variable-sized object may not be initialized" with the following code?

int boardAux[length][length] = {{0}};

推荐答案

我假设您使用的是 C99 编译器(支持动态大小的数组).您代码中的问题是,当编译器看到您的变量声明时,它无法知道数组中有多少个元素(我也在这里假设,从编译器错误中 length 不是编译时间常数).

I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length is not a compile time constant).

您必须手动初始化该数组:

You must manually initialize that array:

int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );

这篇关于C 编译错误:“可能无法初始化可变大小的对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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