可以预处理器自动生成N个数组元素吗? [英] can preprocessor automatically make N array elements?

查看:58
本文介绍了可以预处理器自动生成N个数组元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让预处理器自动生成请求的数组元素数量。每个元素都是零。元素

被粘贴到一个更大的数组中。其他元素可能不为零。


*****这是我需要做的一个例子:


#定义YEAR_1 2005

#define YEAR_2 2007

#define YEARS(YEAR_2 - YEAR_1 + 1)


//开始缺少步骤(以某种方式使用##运算符?)

//这是我无法弄清楚的部分。


//我想以下定义可能需要在

缺失的步骤中

//我想将ZEROES_AND_COMMAS(YEARS)翻译成其中一个


#define ZEROES_AND_COMMAS_1 0

#define ZEROES_AND_COMMAS_2 0,0

#define ZEROES_AND_COMMAS_3 0,0,0


//结束缺少​​的步骤


#define ARRAY_ELEMS_AGE 0,1,5,10,15,20,40,60

#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS(年)

#define ARRAY_ELEMS_EDU 0,6,8,12,16

int intArray [] = {ARRAY_ELEMS_AGE,ARRAY_ELEMS_YRS,

一个RRAY_ELEMS_EDU,};


// intArray结束{0,1,5,10,15,20,40,60,0,0,0,

0,6,8,12,16,}


******这里有一个失败的测试程序(不编译)


#include< stdio.h>

#include< stdlib.h>


#define YEAR_1 2005

#define YEAR_2 2007

#define YEARS(YEAR_2 - YEAR_1 + 1)


#define CAT(x,y)x ## y $ / $
#define XCAT(x,y)CAT(x,y)


#define ZEROES_AND_COMMAS_1 0

#define ZEROES_AND_COMMAS_2 0,0 /
#define ZEROES_AND_COMMAS_3 0,0,0

#define ZEROES_AND_COMMAS(n)XCAT(ZEROES_AND_COMMAS_,n)

>
main(){

int intArray [] = {ZEROES_AND_COMMAS(YEARS)};

printf(" element of elements =%d \ n" ;,sizeof(intArray)/ sizeof

(int));

}


这实际上很有用。我非常熟悉C

预处理器。我已经对##运算符进行了很多实验,并且不能让它完成上述任务,包括与K + R页面相关的实验

231关于使用第二级宏定义。任何人都可以解释

如何解决这个问题?或者让测试程序编译并且

产生正确的阵列长度输出?


谢谢,

Daniel Goldman

I would like to have the preprocessor automatically generate the
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be non-zero.

***** Here is an example of what I need to do:

#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)

// Start missing steps (using ## operator in some way?)
// This is the part I can''t figure out.

// I think the following definitions are probably needed in the
missing steps
// I''d like ZEROES_AND_COMMAS (YEARS) to get translated to one of them

#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0

// End missing steps

#define ARRAY_ELEMS_AGE 0, 1, 5, 10, 15, 20, 40, 60
#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS (YEARS)
#define ARRAY_ELEMS_EDU 0, 6, 8, 12, 16

int intArray [] = { ARRAY_ELEMS_AGE , ARRAY_ELEMS_YRS ,
ARRAY_ELEMS_EDU , };

// intArray ends up with { 0, 1, 5, 10, 15, 20, 40, 60, 0, 0, 0,
0, 6, 8, 12, 16, }

****** Here''s one failed test program (does not compile)

#include <stdio.h>
#include <stdlib.h>

#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)

#define CAT(x,y) x ## y
#define XCAT(x,y) CAT(x,y)

#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0

#define ZEROES_AND_COMMAS(n) XCAT(ZEROES_AND_COMMAS_, n)

main () {
int intArray [] = { ZEROES_AND_COMMAS (YEARS) };
printf ("Number of elements = %d\n", sizeof (intArray) / sizeof
(int));
}

This would actually be useful. I''m very familiar with the C
preprocessor. I''ve experimented with the ## operator a lot, and can''t
make it do the above task, including experiments related to K+R page
231 about using a second level of macro definition. Can anyone explain
how to solve this problem? Or get the test program to compile and
produce correct array length output?

Thanks,
Daniel Goldman

推荐答案

是的预处理器可以生成数组元素以及复杂的

模板:

< a rel =nofollowhref =http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/89382dc9a40439c7target =_ blank> http://groups.google.com /group/comp....382dc9a40439c7

:^ 0

Yes the preprocessor can generate array elemtnes along with complicated
templates:

http://groups.google.com/group/comp....382dc9a40439c7
:^0


dan说:
dan said:

我想让预处理器自动生成

数量的请求数组元素。每个元素都是零。元素

被粘贴到一个更大的数组中。其他元素可以是非零的。
I would like to have the preprocessor automatically generate the
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be non-zero.



编写并使用预处理器。也就是说,编写一个从文件中获取简单指令的程序,然后写出一个完全符合您需求的.h文件

。然后运行该程序。然后#include

结果.h到你的代码中,你就完成了。


-

Richard Heathfield < http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Write, and use, a prepreprocessor. That is, write a program which takes
simple instructions from a file, and which then writes out a .h file
that suits your needs exactly. Then run the program. Then #include the
resulting .h into your code, and you''re done.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


dan< da ******* @ yahoo.comwrites:
dan <da*******@yahoo.comwrites:

我想让预处理器自动生成请求的数组元素数量。每个元素都是零。元素

被粘贴到一个更大的数组中。其他元素可能不为零。


*****这是我需要做的一个例子:


#定义YEAR_1 2005

#define YEAR_2 2007

#define YEARS(YEAR_2 - YEAR_1 + 1)


//开始缺少步骤(以某种方式使用##运算符?)

//这是我无法弄清楚的部分。


//我想以下定义可能需要在

缺失的步骤中

//我想将ZEROES_AND_COMMAS(YEARS)翻译成其中一个


#define ZEROES_AND_COMMAS_1 0

#define ZEROES_AND_COMMAS_2 0,0

#define ZEROES_AND_COMMAS_3 0,0,0


//结束缺少​​的步骤


#define ARRAY_ELEMS_AGE 0,1,5,10,15,20,40,60

#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS(年)

#define ARRAY_ELEMS_EDU 0,6,8,12,16

int intArray [] = {ARRAY_ELEMS_AGE,ARRAY_ELEMS_YRS,

ARRAY_ELE MS_EDU,};


// intArray结束{0,1,5,10,15,20,40,60,0,0,0,

0,6,8,12,16,}
I would like to have the preprocessor automatically generate the
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be non-zero.

***** Here is an example of what I need to do:

#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)

// Start missing steps (using ## operator in some way?)
// This is the part I can''t figure out.

// I think the following definitions are probably needed in the
missing steps
// I''d like ZEROES_AND_COMMAS (YEARS) to get translated to one of them

#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0

// End missing steps

#define ARRAY_ELEMS_AGE 0, 1, 5, 10, 15, 20, 40, 60
#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS (YEARS)
#define ARRAY_ELEMS_EDU 0, 6, 8, 12, 16

int intArray [] = { ARRAY_ELEMS_AGE , ARRAY_ELEMS_YRS ,
ARRAY_ELEMS_EDU , };

// intArray ends up with { 0, 1, 5, 10, 15, 20, 40, 60, 0, 0, 0,
0, 6, 8, 12, 16, }



你没有看到反对C99,按你的评论语法,所以有可能

是另一种方式:


int intArray [] = {ARRAY_ELEMS_AGE,[N_AGES + YEARS] = ARRAY_ELEMS_EDU};


您可以将索引部分引入初始化列表。对于

这项工作,你需要知道(作为一个常量表达式)

ARRAY_ELEMS_AGE元素的数量(或者当然,总大小和数字
培训EDU元素。)


未明确初始化的所有元素都设置为零(提供

至少有一个值显式值在列表中)。


-

Ben。

You don''t see averse to C99, going by your comment syntax, so there may
be another way:

int intArray [] = { ARRAY_ELEMS_AGE, [N_AGES+YEARS] = ARRAY_ELEMS_EDU };

You can introduce an index part way into an initialiser list. For
this work, you need to know (as a constant expression) the number of
ARRAY_ELEMS_AGE elements (or, of course, the total size and the number
of training EDU elements).

All elements not explicitly initialised are set to zero (provided
there is at least one value explicit value in the list).

--
Ben.


这篇关于可以预处理器自动生成N个数组元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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