静态const int的不是数组大小不够好? [英] Static const int not good enough for array size?

查看:220
本文介绍了静态const int的不是数组大小不够好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static const int size = 5;
@interface Foo { char bar[size]; }

但是,编译器抱怨的实例变量必须有固定的大小。难道我真的要的#define 的大小,或者是有办法得到它与常规的恒定的工作吗? (我想是静态分配的内存,没有的malloc

But the compiler complains that "instance variables must have constant size". Do I really have to #define the size, or is there a way to get it work with a regular constant? (I’d like the memory to be allocated statically, no malloc.)

推荐答案

有可能是您的Objective-C编译器使用C编译器作为后端。 C(高达C98)只允许常量前pressions作为数组的大小。这是你的编译器抱怨。所以,你没有得到使用的标识符作为数组的大小。 (简而言之,如果preprocessor无法计算数量,它不是去上班。)所以,是的,你会得到使用的#define。

It is likely that your Objective-C compiler uses a C compiler as a backend. C (up to C98) only permits constant expressions as array sizes. This is what your compiler is complaining about. So you don't get to use an identifier as an array size. (In short, if the preprocessor can't compute the number, it's not going to work.) So, yes, you're going to get to use a #define.

这篇关于静态const int的不是数组大小不够好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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