在C中我可以声明一个常量字符数组吗?

发布于 2024-12-07 20:37:57 字数 817 浏览 0 评论 0原文

我正在研究一个字符串来表示符号的变化。我已经成功使用了下面注释掉的字符串,但更喜欢使用常量字符数组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆 2024-12-14 20:37:57

如果您稍后不修改该字符串,您可以这样做:

const char *direction:
if (value - price1 > - 0.005) { // adjusts for noise near 0
    direction = UP;
}
else
    direction = DOWN;

If you're not modifying the string later, you could do it like this:

const char *direction:
if (value - price1 > - 0.005) { // adjusts for noise near 0
    direction = UP;
}
else
    direction = DOWN;
路弥 2024-12-14 20:37:57

你不能这样分配,但你可以这样做:

strcpy(direction, UP);

strcpy(direction, DOWN);

显然,要小心不要溢出缓冲区。如果这些是唯一可能的来源,那就没问题。

You can't assign like that, but you can do:

strcpy(direction, UP);

strcpy(direction, DOWN);

Obviously, be careful not to overflow your buffer. If those are the only possible sources, you're fine.

有木有妳兜一样 2024-12-14 20:37:57

考虑使用:

const char up_string[] = "UP";
const char down_string[] = "DOWN";
char *direction;
direction = (value - price1 > - 0.005) ? up_string : down_string;

然后,您可以将方向简单地设置为指向这些位置之一的指针(而不是使用 strcpy)。

Consider using:

const char up_string[] = "UP";
const char down_string[] = "DOWN";
char *direction;
direction = (value - price1 > - 0.005) ? up_string : down_string;

You could then have direction simply be a pointer to either of those locations (as opposed to using strcpy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文