在C文件范围内动态可变数组 [英] variably modified array at file scope in C

查看:170
本文介绍了在C文件范围内动态可变数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code是这样的:

I have some code like this:

static int a = 6;
static int b = 3;

static int Hello[a][b] =
{
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3},
    { 1,2,3}
};

但是当我编译它,它说错误:

but when I compile it, it says error:

在文件范围内动态可变你好

variably modified 'Hello' at file scope

怎么会发生这种事?我怎么能解决这个问题?

how could this happen? and how could I fix it?

推荐答案

您不能有静态数组的大小而给出一个变量

You can not have static array which size is given as a variable

这就是为什么常量应的#define D:

That's why constants should be #defined:

#define a 6

此方法preprocessor将取代 A 6 ,使其成为有效的申报。

This way preprocessor will replace a with 6, making it valid declaration.

这篇关于在C文件范围内动态可变数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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