gpt4 book ai didi

c - C 中的加载结构

转载 作者:行者123 更新时间:2023-11-30 18:36:34 26 4
gpt4 key购买 nike

在我的头文件中,我定义了一些结构:

typedef struct { double X; } feature_t;

typedef struct
{
int n;
feature_t *Features;
float *Weights;
} signature_t;

在我的主代码中,我声明了一些新结构并尝试加载它们:

feature_t  *f_x;
int i;

memset(f_x,0,sizeof(feature_t)*n_x);

for(i=0; i<n_x; i++){
f_x[i] = 100.0;
w_x[i] = (float)p_x[i];
}

当我编译这个时,出现以下错误:

error: incompatible types when assigning to type ‘feature_t {aka struct <anonymous>}’ from type ‘double’

为什么编译器无法将此 double 值加载到此字段中?我将其声明为 double 。

谢谢!

最佳答案

存在编译问题和运行时问题,一旦编译问题解决,就需要修复。

编译问题是您无法将 float 转换为 struct,即使 struct 的唯一字段是 > float 。您需要像这样分配它:

f_x[i].X = ... // some float expression here

运行时问题是 feature_t *f_x; 指针不能像数组一样使用,除非您为其分配一些内存。回想一下,指针不是数组。你需要做这样的事情:

feature_t  *f_x = malloc(sizeof(*f_x)*n_x);

关于c - C 中的加载结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40581598/

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