c - 数组结构体和数组

标签 c

我写了一些代码:)。我不知道让它发挥作用。当我使用数组时它可以工作,但当我使用结构数组时它不起作用。在我的程序中,我使用库函数从数据库内部获取数据,它看起来像这样:

int opfGetAll( void *opf,const char *info, const char *name, void *parms )

opf - 指向 db 的指针
info - 信息类型
name - 参数名称
parm - 数组输出,必须是正确的类型

int main()
{
    struct data
    {
        int intVal;
        float floatVal;
        double doubleVal;
     };

     //struct data *iform1_tab = malloc(sizeof(struct data)*number_locs);
     float *iform1_tab = malloc(sizeof(float)*number_locs);
     if(iform1 ==2 )//FLOAT
     {
         //opfGetAll(opf,type1,attr_inp1, &iform1_tab[0].floatVal);//don't take all data, it fill half of it and the rest is 0 or rubbish
         opfGetAll(opf,type1,attr_inp1, iform1_tab );//this version fill all array

     }


 }

最佳答案

需要了解(并发布)有关函数 opfGetAll() 内部结构的更多信息,才能获得更完整的答案。第四个参数,void *,意味着该函数可能被设置为处理不止一种类型的输入。是否包括 flaot * 尚不清楚,但从您正在尝试的内容来看,并且没有更准确的文档,您的假设似乎是可以的。

但是,根据您的评论,您调用函数的两种方式如下:

 //opfGetAll(opf,type1,attr_inp1, &iform1_tab[0].floatVal);//don't take all data, it fill half of it and the rest is 0 or rubbish
 opfGetAll(opf,type1,attr_inp1, iform1_tab );//this version fill all array

强烈建议opfGetAll的第四个参数需要某种形式的struct data *,无论它是单个实例还是数组。如果是这种情况,这就是为什么您在尝试传递其他内容时会看到意外结果的原因。 (即&iform1_tab[0].floatVal,它是一个float *)

关于c - 数组结构体和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974450/

相关文章:

结合 GetLastError 的值和自定义错误消息

c - 包含来自另一个目录的头文件

c - 这个程序使用多少 inode,打开文件表条目和文件描述符

c - 如何将数据从 C 导出到 MATLAB(在不同机器上)

c - 错误: subscripted value is neither array nor pointer

c - rgb led arduino 的平滑动画

c - 如何解决c中的超时错误?

c - 我如何在 Linux 中表示成功状态代码

c++ - 使用范围解析运算符传递函数指针arduino

c - 为什么 clang 找不到 unixodbc 函数的符号?