从另一个数组值初始化数组大小 [英] initialize array size from another array value

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

问题描述

#include<iostream> 
using namespace std; 

const int vals[] = {0, 1, 2, 3, 4}; 

int newArray[ vals[2] ]; //"error: array bound is not an integer constant"

int main(){
    return vals[2];
}

//returns 2 if erroneous line is removed

为什么不这项工作?

Why doesn't this work?

推荐答案

C ++编译器只能在编译时已知的大小分配的数组。如果你想分配可变大小一块内存,使用运营商。

The C++ compiler can only allocate an array with a size known at compile time. If you want to allocated a variable size piece of memory, use the new operator.

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

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