错误:可变大小的物体可能无法初始化 [英] Error: Variable-sized object may not be initialized

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

问题描述

 的#define L 1
#定义M L + 1诠释主要(无效){    INT N = L * M * 2;
    炭S [N] = {'1','2'};
    的printf(%D的sizeof(S));    返回1;
}

为什么上面的code不能编译? Eclipse的编译器说两件事情:


  1. 可变大小的物体可能无法初始化:的是,因为氮以M的定义和L在宏观还是因为我不能用的初始化数组类型改编[x]其中x是在任何情况下变量?

  2. 在数组初始化多余的元素的 - 什么意思


解决方案

  

因为我不能初始化型ARR数组[x]其中x是变量?


事实上,pre-C99,数组的大小必须是一个的恒前pression 的。在C99,这已经放宽用的变长数组

(这是无关的宏)


  

在阵列initializer-是什么意思多余的元素?


解决的第一个问题,这应该走了。

#define L 1
#define M L+1

int main(void){

    int N=L*M*2;
    char s [N]={'1','2'};
    printf("%d", sizeof(s));

    return 1;   
}

Why does the above code cannot be compiled? The compiler of Eclipse says two things:

  1. Variable-sized object may not be initialized: Is because the definition of N with M and L at the macro or is it because I can't initialize an array with type arr[x] where x is variable under any circumstances?
  2. excess elements in array initializer- What does it mean?

解决方案

because I can't initialize an array with type arr[x] where x is variable?

Indeed, pre-C99, the array size must be a constant expression. In C99, this has been relaxed with variable-length arrays.

(This is is nothing to do with macros.)

excess elements in array initializer- What does it mean?

Fix the first problem, and this should go away.

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

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