数组初始化问题。 [英] Array Initialization problems.
问题描述
亲爱的:
我有以下问题。如果我想使用
括号初始化数组,有没有办法可以定义它们并在不同的行中给出初始的
值,例如:
int X [5];
X [5] = {0,0,1,2,9};
但这给了我错误信息。我知道int X [5] = {0,0,1,2,9};"
有效。
我真的很想这样做因为这个是一个公共数组,将是许多子程序使用的
。
谢谢!
此致,
Trevor
Hi,dear all:
I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:
int X[5];
X[5] = {0,0,1,2,9};
But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.
Thanks!
Sincerely,
Trevor
推荐答案
Trevor Qi写道:
Trevor Qi wrote:
亲爱的:
<我有以下问题。如果我想使用
大括号初始化数组,有没有办法可以定义它们并在不同的行中给出初始的值,例如:
int X [5 ];
X [5] = {0,0,1,2,9};
Hi,dear all:
I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:
int X[5];
X[5] = {0,0,1,2,9};
据我所知,即使在C99中也没有。
解释为什么要用两行来做这件事。
vir
As I know, there isn''t, even in C99.
Explain any reaseon why you want to do it in two lines.
vir
< br>
qh****@hotmail.com (Trevor Qi)写的
新闻:17 ************************** @ posting.google.c om:
qh****@hotmail.com (Trevor Qi) wrote in
news:17**************************@posting.google.c om:
亲爱的:
我有以下问题。如果我想使用
大括号初始化数组,有没有办法可以定义它们并在不同的行中给出初始的值,例如:
int X [5 ];
X [5] = {0,0,1,2,9};
但是这给了我错误信息。我知道int X [5] = {0,0,1,2,9};"
有效。
我真的想这样做,因为这是一个公共数组,将会<许多子程序使用过。
Hi,dear all:
I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:
int X[5];
X[5] = {0,0,1,2,9};
But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.
然后不,你不能。你可以做一些愚蠢的事情:
x [0] = 0,x [1] = 0,x [2] = 1,x [3] = 2,x [4] = 9;
但这可能不会扩展得太好。如果x []是
文件范围,你可以创建一个函数级数组和memcpy()它。
例如
#include< string.h>
int x [5];
int main(无效)
{
int private_x [] = {0,0,1,2,9};
memcpy(x,private_x,sizeof x);
返回0;
}
-
- 马克 - > ;
-
Then no, you cannot. You could do something silly like:
x[0] = 0, x[1] = 0, x[2] = 1, x[3] = 2, x[4] = 9;
but that''s probably not something that will scale too well. If x[] is
file-scoped you could create a function level array and the memcpy() it.
E.g.
#include <string.h>
int x[5];
int main(void)
{
int private_x[] = { 0, 0, 1, 2, 9 };
memcpy(x, private_x, sizeof x);
return 0;
}
--
- Mark ->
--
----- BEGIN PGP签名消息-----
哈希: SHA1
Trevor Qi写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Trevor Qi wrote:
亲爱的:
我有以下问题。如果我想使用
大括号初始化数组,有没有办法可以定义它们并在不同的行中给出初始的值,例如:
int X [5 ];
X [5] = {0,0,1,2,9};
除非你使用印刷休息而不是声明休息,否则不会。
你必须做类似的事情
int X [5]
= {0,0,1,2,9};
否则,你是什么想做的事情是不可能的,你的代码示例是
不正确。
但这给了我错误信息。我知道int X [5] = {0,0,1,2,9};"
有效。
我真的想这样做,因为这是一个公共数组,将会<许多子程序使用过它。
谢谢!
真诚的,
Trevor
Hi,dear all:
I have the following question. If I want to initialize an array using
braces, is there a way that I can define them and give the initial
value in different lines, like:
int X[5];
X[5] = {0,0,1,2,9};
Not unless you use a typographical break rather than a statement break.
You''d have to do something like
int X[5]
= {0,0,1,2,9};
Otherwise, what you want to do is not possible, and your code sample is
incorrect.
But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
works.
I really want to do this because this is a public array and will be
used by many Subroutines.
Thanks!
Sincerely,
Trevor
- -
Lew Pitcher,IT顾问,企业应用程序架构
企业技术解决方案,道明银行金融集团
(意见表达这是我自己的,而不是我的雇主'
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.2.4(MingW32)
iD8DBQFApQjtagVFX4UWr64RAgQiAJ0XI + 5JF4zrFcV5KOHw8r q2SnOgUQCg3qUf
62gOL0i9VLRUrwkhV ++ G1zY =
= Z3w1
--- --END PGP SIGNATURE -----
- --
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer''s)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFApQjtagVFX4UWr64RAgQiAJ0XI+5JF4zrFcV5KOHw8r q2SnOgUQCg3qUf
62gOL0i9VLRUrwkhV++G1zY=
=Z3w1
-----END PGP SIGNATURE-----
这篇关于数组初始化问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!