结构数组的静态(非零)初始化(长度为#define LEN的数组) [英] static (non-zero) initialization of an array of structs (array of length #define LEN)
问题描述
您好!
在以下代码片段中,是否可以使用STRUCT_INIT初始化arr的每个
元素?
< br $>
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5,'''}
#define LEN 3
struct mystruct arr [LEN] = \ < br $>
{STRUCT_INIT};
/ *不起作用:只按需要初始化第一个元素* /
/ *
我不想写。
{STRUCT_INIT,STRUCT_INIT,STRUCT_INIT};
因为LEN可以改变!
!! !!!!!!!
* /
..
..
可以这样做了吗?
谢谢
-Albert
..
。 。
..
..
/ ****** test-program ****** /
#define< stdio.h>
int main(无效)
{
int i;
for(i = 0; i< LEN; i ++)
printf(" | |%d |,|%c | \ n",arr [i] .a,arr [i] .b);
返回0;
}
Hello!
In the following code-snippet, is it possible to initialize each
element of arr, with STRUCT_INIT?
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5, ''a''}
#define LEN 3
struct mystruct arr[LEN] = \
{STRUCT_INIT};
/* does not work: only 1st element initialized as desired */
/*
I don not want to write
{STRUCT_INIT, STRUCT_INIT, STRUCT_INIT};
since LEN can change!
!!!!!!!!!
*/
..
..
Can this be done?
Thanks
-Albert
..
..
..
..
/****** test-program ******/
#define <stdio.h>
int main(void)
{
int i;
for (i = 0; i < LEN; i++)
printf("|%d|, |%c|\n", arr[i].a, arr[i].b);
return 0;
}
推荐答案
8月27日下午1:02,anon.a ... @ gmail.com写道:
On Aug 27, 1:02 pm, anon.a...@gmail.com wrote:
你好!
在下面的代码片段中,是否可以初始化每个
arr的元素,STRUCT_INIT?
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5,''''}
#define LEN 3
struct mystruct arr [LEN] = \
{STRUCT_INIT};
/ *不起作用:只有第一个元素按需要初始化* /
/ *
我不想写
{STRUCT_INIT,STRUCT_INIT,STRUCT_INIT};
因为LEN可以改变!
!!!!!!!!!
* /
Hello!
In the following code-snippet, is it possible to initialize each
element of arr, with STRUCT_INIT?
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5, ''a''}
#define LEN 3
struct mystruct arr[LEN] = \
{STRUCT_INIT};
/* does not work: only 1st element initialized as desired */
/*
I don not want to write
{STRUCT_INIT, STRUCT_INIT, STRUCT_INIT};
since LEN can change!
!!!!!!!!!
*/
也许它可以通过预处理器来完成。
我尝试了一下,但是我不是预处理专家 - 下面的代码
导致编译器(预处理器)错误。
但是:可以这样做:
#define STRUCT_INIT {5,''a''}
#define PUT_A(n)\
#if ((n)== 1)\
STRUCT_INIT \
#elif((n)0)\
STRUCT_INIT,PUT_B ((n)-1)\
#endif
#define PUT_B(n)\
#if( (n)== 1)\
STRUCT_INIT \
#elif((n)0)\
STRUCT_INIT,PUT_A( (n)-1)\
#endif
#define LEN 3
struct mystruct arr [LEN] = \
{PUT_B(LEN)};
谢谢--Albert
Perhaps it can be done with the preprocessor.
I gave it a try, but I''m no preprocessor expert - the code below
results in compiler (preprocessor) errors.
But: Could something along these lines, be done:
#define STRUCT_INIT {5, ''a''}
#define PUT_A(n) \
#if ((n) == 1) \
STRUCT_INIT \
#elif ((n) 0) \
STRUCT_INIT , PUT_B((n)-1) \
#endif
#define PUT_B(n) \
#if ((n) == 1) \
STRUCT_INIT \
#elif ((n) 0) \
STRUCT_INIT , PUT_A((n)-1) \
#endif
#define LEN 3
struct mystruct arr[LEN] = \
{PUT_B(LEN)};
Thanks -Albert
******* @ gmail.com 写道:
an*******@gmail.com writes:
8月27日下午1:02,anon.a ... @ gmail.com写道:
On Aug 27, 1:02 pm, anon.a...@gmail.com wrote:
>在下面的代码片段中,是否可以使用STRUCT_INIT初始化arr的每个
元素?
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5,''a''}
#define LEN 3
struct mystruct arr [LEN] = \
{STRUCT_INIT} ;
>In the following code-snippet, is it possible to initialize each
element of arr, with STRUCT_INIT?
struct mystruct {
int a;
char b;
};
#define STRUCT_INIT {5, ''a''}
#define LEN 3
struct mystruct arr[LEN] = \
{STRUCT_INIT};
< snip>
<snip>
>
也许可以使用预处理器完成。
>
Perhaps it can be done with the preprocessor.
< snip>
<snip>
#define STRUCT_INIT {5,'a''}
#define PUT_A(n)\
#if((n)== 1)\
STRUCT_INIT \
#elif((n)0)\
STRUCT_INIT,PUT_B((n)-1)\
#endif
#define STRUCT_INIT {5, ''a''}
#define PUT_A(n) \
#if ((n) == 1) \
STRUCT_INIT \
#elif ((n) 0) \
STRUCT_INIT , PUT_B((n)-1) \
#endif
< snip>
你正在付出很多努力来避免最简单的for循环。为什么?
只需循环初始化您的数据。
-
Ben。
<snip>
You are putting a lot of effort to avoid the simplest for loop. Why?
Just initialise your data in a loop.
--
Ben.
8月27日下午1:47,Ben Bacarisse< ben.use ... @ bsb.me.ukwrote:
< snip>
On Aug 27, 1:47 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
<snip>
你正在付出很多努力来避免最简单的for循环。为什么?
You are putting a lot of effort to avoid the simplest for loop. Why?
好问题 - 它让我思考!
这里有一个场景:如果你的代码在启动时运行并想要
尽可能快地启动!
Good question - it made me think!
Here''s a scenario: If you have code running at boot time and want to
boot as fast as possible!
只需在循环中初始化数据。
Just initialise your data in a loop.
True。 -Albert
True. -Albert
这篇关于结构数组的静态(非零)初始化(长度为#define LEN的数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!