gpt4 book ai didi

c - 如何将数组初始化为非数字值

转载 作者:行者123 更新时间:2023-12-02 05:51:01 24 4
gpt4 key购买 nike

我正在尝试初始化一个数组,这样我就可以使用一个简单的 if 语句来检查一个值是否已放入数组中。

这可能吗?

这是我的代码

double number[1024] = {non-numeric value}
int i = 0;
while(1){
if (number[i] != non-numeric value){

printf ("%f", number[i]);
i++;
}
else
break;

}

最佳答案

要将数据初始化为 NaN,请使用 NAN <math.h> 中定义的宏

#include <math.h>

// initialize to all NaNs
for (int i=0; i<1024; ++i) number[i] = NAN;

要检查 NaN,请使用 isnan来自 <math.h> 的宏

if (!isnan(number[i])) {
// do something if the value isn’t NaN.
}

关于c - 如何将数组初始化为非数字值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15907384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com