数组结构和数组

时间:2019-09-17 12:41:41

标签: 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

     }


 }

1 个答案:

答案 0 :(得分:1)

需要进一步了解(并发布)有关函数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的第4个参数期望某种形式的struct data *,无论它是单个实例还是数组。如果真是这样,这就是为什么您尝试传递其他内容时看到意外结果的原因。 (即&iform1_tab[0].floatVal,即float *