gpt4 book ai didi

C、memset双数组失败

转载 作者:行者123 更新时间:2023-11-30 20:43:52 25 4
gpt4 key购买 nike

我想动态声明一个 double 类型数组,所以这是我的代码

void function(int length, ...)
{
...

double *a = malloc(sizeof(double) * length);
memset(a, 1, sizeof(double) * length);
for (int i = 0; i < length; i++)
{
printf("%f", a[i]);
}

...
}

当我传递 length2 时,代码不会打印全 1。它只打印以下内容:

7.7486e-304
7.7486e-304

那么,我应该做什么来解决这个问题?

最佳答案

memset 设置字节。你正试图设置 double 。只需从 0 循环到 length 并将每个值设置为 1.0:

for (int i = 0; i < length; i ++)
{
a[i] = 1; // or 1.0 if you want to be explicit
}

关于C、memset双数组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549564/

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