在C中我可以声明一个常量字符数组吗?
我正在研究一个字符串来表示符号的变化。我已经成功使用了下面注释掉的字符串,但更喜欢使用常量字符数组 UP =“up/0/0”和 DOWN =“down”的简单 if-else 语句。
有谁知道声明此类常量值的简单方法?
char direction[5]; // declares char array to signify sign change
if (value - price1 > - 0.005) { // adjusts for noise near 0
direction = UP;
}
else direction = DOWN;
// update price-change direction string
// if (value - price1 > - 0.005) { // adjusts for noise near 0
// direction[0] = 'u';
// direction[1] = 'p';
// direction[2] = 00; // set character to null value
// direction[3] = 00;
// }
//
// int[4] var_name
// else {
// direction[0] = 'd';
// direction[1] = 'o';
// direction[2] = 'w';
// direction[3] = 'n';
// }
I am working on a character string to signify a change in sign. I have had success with the character string that's commented out below, but would prefer a simple if-else statement using constant character arrays UP = "up/0/0" and DOWN = "down".
Does anyone know a simple way to declare such constant values?
char direction[5]; // declares char array to signify sign change
if (value - price1 > - 0.005) { // adjusts for noise near 0
direction = UP;
}
else direction = DOWN;
// update price-change direction string
// if (value - price1 > - 0.005) { // adjusts for noise near 0
// direction[0] = 'u';
// direction[1] = 'p';
// direction[2] = 00; // set character to null value
// direction[3] = 00;
// }
//
// int[4] var_name
// else {
// direction[0] = 'd';
// direction[1] = 'o';
// direction[2] = 'w';
// direction[3] = 'n';
// }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您稍后不修改该字符串,您可以这样做:
If you're not modifying the string later, you could do it like this:
你不能这样分配,但你可以这样做:
显然,要小心不要溢出缓冲区。如果这些是唯一可能的来源,那就没问题。
You can't assign like that, but you can do:
Obviously, be careful not to overflow your buffer. If those are the only possible sources, you're fine.
考虑使用:
然后,您可以将方向简单地设置为指向这些位置之一的指针(而不是使用 strcpy)。
Consider using:
You could then have direction simply be a pointer to either of those locations (as opposed to using strcpy).