C预处理器:在C中定义。。。劝告
我正在做一个大型的C项目,我以前从未遇到过这样的情况,所以我需要你的建议 你的意见是什么?像我在下面所做的那样,在条件预处理器中定义常量可以吗?或者你建议我以其他方式这样做C预处理器:在C中定义。。。劝告,c,c-preprocessor,C,C Preprocessor,我正在做一个大型的C项目,我以前从未遇到过这样的情况,所以我需要你的建议 你的意见是什么?像我在下面所做的那样,在条件预处理器中定义常量可以吗?或者你建议我以其他方式这样做 #define NUM_OCTAVES_4 //#define NUM_OCTAVES_5 #ifdef NUM_OCTAVES_4 #define OCTAVES 4 const unsigned char borders [4] = {11, 26, 50, 98}; #elif N
#define NUM_OCTAVES_4
//#define NUM_OCTAVES_5
#ifdef NUM_OCTAVES_4
#define OCTAVES 4
const unsigned char borders [4] = {11, 26, 50, 98};
#elif NUM_OCTAVES_5
#define OCTAVES 5
const unsigned char borders [5] = {11, 26, 50, 98, 194};
#endif
这样做有什么缺点吗?当然可以。这就是条件编译的要点。八度音阶4/5是否如此基本,以至于您需要编译不同版本的应用程序?
#define OCTAVES 4
#if OCTAVES == 4
const unsigned char borders [4] = {11, 26, 50, 98};
#elif OCTAVES == 5
const unsigned char borders [5] = {11, 26, 50, 98, 194};
#endif
如果你在销售它,它会是另一种产品吗
正确的方法是使用malloc在运行时分配边界,但如果这只是一个简单的练习,您可能还不想了解这一点
或者您可以制作borders[5],并将最后一个值设置为0或一些易于检测的八度音阶结束值。为什么不只使用5版本的数组,然后使用 int-noctaves=4;或者,如果您只想要4度,可以通过其他方式忽略最后一个八度?更聪明一点,即重复性更少:
#define OCTAVES 4
/*define OCTAVES 5 */
const unsigned char borders [] = {11, 26, 50, 98,
#if OCTAVES == 5
194
#endif
};
这样,您就不需要在代码的两个分支中使用相同的前四个值
或者,如果您发现这种攻击性,请将其分解到宏中,然后重复:
#define BORDERS4 11, 26, 50, 98
#if OCTAVES == 4
const unsigned char borders[] = { BORDERS4 };
#else if OCTAVES == 5
const unsigned char borders[] = { BORDERS4, 198 };
#endif
如果将其更改为const unsigned char border[OCTAVES]=则会更酷。否则应为elif。