引用函数外部的全局数组 [英] Referencing a global array outside a function
问题描述
我需要创建一个全局数组,其维度取决于初始化时填充的另一个全局数组的
内容。对于
示例:
int array1 [3] = {3,2,1};
int array2 [array1 [ 0]]; //应该是等价的to:int array2 [3];
GCC给出了这些错误:
在任何函数之外声明的可变大小类型
可能无法初始化可变大小的对象
警告:数组初始化程序中的多余元素
警告:(接近初始化`array2'')
两个数组都需要是全局的,并在main()启动之前创建。任何
想法?
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:
int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];
GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2'')
Both arrays need to be global and created before main() starts. Any
ideas?
推荐答案
2004年5月8日07:21:09 -0700, fl ****** @ easy.com (flipflop)写道:
On 8 May 2004 07:21:09 -0700, fl******@easy.com (flipflop) wrote:
我需要创建一个全局数组,其维度取决于初始化时填充的另一个全局数组的内容。对于
示例:
int array1 [3] = {3,2,1};
int array2 [array1 [0]]; //应该是等价的to:int array2 [3];
GCC给出了这些错误:
在任何函数之外声明的变量大小类型
可变大小的对象可能无法初始化
警告:数组初始化程序中的多余元素
警告:(接近初始化`array2'')
两个数组都需要是全局的并且在main()启动之前创建。任何
想法?
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:
int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];
GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2'')
Both arrays need to be global and created before main() starts. Any
ideas?
#include< stdio.h>
#define arrsize(a)( sizeof(a)/ sizeof(* a))
int array1 [3] = {3,2,1};
int array2 [arrsize( ARRAY1)]; / *应该是等价的。 to:int array2 [3]; * /
int main()
{
printf(" sizeof(array1)=%d \ n" ,sizeof(array1));
printf(" sizeof(array2)=%d \ n",sizeof(array2));
返回0;
}
输出:
sizeof(array1)= 12
sizeof(array2)= 12
-leor
-
Leor Zolman --- BD软件--- www.bdsoft.com
C / C ++,Java,Perl和Unix的现场培训
C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html
2004-05-08,flipflop< fl * *****@easy.com>写道:
On 2004-05-08, flipflop <fl******@easy.com> wrote:
我需要创建一个全局数组,其维度取决于初始化时填充的另一个全局数组的内容。对于
示例:
int array1 [3] = {3,2,1};
int array2 [array1 [0]]; //应该是等价的to:int array2 [3];
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:
int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];
#define ARRAY1_MAX(3)
#define ARRAY1(n)(ARRAY1_MAX - (n) )
int array1 [3] = {ARRAY1(0),ARRAY1(1),ARRAY1(2)};
int array2 [ARRAY1(0 )];
- 詹姆斯
#define ARRAY1_MAX (3)
#define ARRAY1(n) (ARRAY1_MAX - (n))
int array1[3] = { ARRAY1(0), ARRAY1(1), ARRAY1(2) };
int array2[ARRAY1(0)];
-- James
flipflop写道:
flipflop wrote:
我需要创建一个全局数组的大小取决于初始化时填充的另一个全局数组的内容。对于
示例:
int array1 [3] = {3,2,1};
int array2 [array1 [0]]; //应该是等价的to:int array2 [3];
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:
int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];
enum {constant_expression = 3};
int array1 [3] = {constant_expression,2, 1};
int array2 [constant_expression];
Jeremy。
enum { constant_expression = 3 };
int array1[3] = { constant_expression, 2, 1};
int array2[constant_expression];
Jeremy.
这篇关于引用函数外部的全局数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!