【问题标题】:Preprocessor #if Array预处理器 #if 数组
【发布时间】:2018-12-29 04:29:51
【问题描述】:

我正在处理大量代码并想在参数文件中进行一些计算,因此我使用的是预处理器语言。

所以

const D_REAL spher2car[3] = { 
    sin(theta)*cos(phi),  
    sin(theta)*sin(phi),
    cos(theta)
}; 

const D_REAL spher2car2[3] = { 
    spher2car[0]+h0*sin(thetap)*cos(phip),
    spher2car[1]+h0*sin(thetap)*sin(phip),
    spher2car[2]+h0*cos(thetap)
}; 

#if (spher2car[2]<spher2car2[2]) 

给我错误

 the token "[" is not valid in preprocessor expressions

所以基本上我必须设置一个不会再改变的值,但在程序启动之前依赖于其他常量参数。

我该怎么做?

【问题讨论】:

  • 确实知道预处理器只是编译时的东西(它甚至会在实际解析器运行之前运行)?并且对变量或其运行时值一无所知?
  • @Someprogrammerdude 我认为这里对const 不可变存在误解。
  • 但显然,即使只是前两行也行不通。

标签: c c-preprocessor preprocessor


【解决方案1】:

预处理器甚至在编译器之前运行。您所拥有的只能在运行时进行评估。所以请改用常规的if 语句:

if (spher2car[2]<spher2car2[2]) {
    // do something
} else {
    // do something else
}

请注意,因为这是必须驻留在函数内部的可执行代码。上面的变量声明也是如此,因为必须对表达式进行求值才能对其进行初始化。

在您执行其他任何操作之前,最好在main 中执行此操作。

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2017-04-19
    • 2014-11-19
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多