0

I wrote some code:). I don't know make it work. It work when I use array but it doesn't work when I use array of structs. In my program I use library function to take data from inside database and it look like that:

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

opf - pointer to db
info - the info type
name - name of parameter
parm - the array output, this must be of the correct type

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

     }


 }
7
  • The array of struct data is overwriten (you loose the array and introduce a memory leak) in the second call to malloc Commented Sep 17, 2019 at 12:44
  • where does iform1 in if(iform1 ==2 ) come from? Commented Sep 17, 2019 at 12:46
  • I don't use this malloc at this same time. I use them interchangeably. This same with opfGetAll:). Commented Sep 17, 2019 at 12:46
  • 1
    Well, first I think we need to know what do you expect from your code ! Commented Sep 17, 2019 at 12:49
  • 1
    Please edit your question and add all relevant information there instead of answering in comments. How does your function opfGetAll know if void *parms points to an array of struct or an array of float and how big your array is? If you pass &iform1_tab[0].floatVal, it cannot know how to find iform1_tab[1].floatVal etc. Commented Sep 17, 2019 at 13:06

1 Answer 1

1

More needs to be known (and posted) about the internals of the function opfGetAll() for a more complete answer. The 4th argument, void *, means the function is likely set up to handle more than one type of input. Whether that includes flaot * is not known, but from what you are trying, and without more accurate documentation, the assumption appears you expect that is can.

However, from your comments following the two ways you have called the functions:

 //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

strongly suggests that the 4th argument of opfGetAll expects some form of struct data *, whether it be a single instance or an array. If that is the case, this would be why you are seeing unexpected results when attempting to pass it something else. (i.e. &iform1_tab[0].floatVal, which is a float *)

Sign up to request clarification or add additional context in comments.

7 Comments

I did not specify the query. I don't use this declaration at the same time.I use them interchangeably. So this is not the problem.
@Kris_Holder - it appears the content of your post has changed from what I posted this against, which changes things. Regardless of those changes, the first malloc call you indicate, is compatible with the form of arguments in the statement you marked as //don't take. Beyond that, edit your question to include specifically what you want.
So the struct version should work. But it doesn't and I don't know why:(.
@Kris_Holder - I just created an instance of your struct, and used its members in a test function with the prototype that includes a float *var as one of its arguments. It worked fine. What is the exact prototype of the function opfGetAll(?) (either reply here, or edit your OP with that information. It is very relevant.) If the 4th argument is not prototyped as float *, then the argument as passed in your first instance (commented now) will not work.
I put the prototype of the function at the top. I don't have more information:(. I Can write that it return 1 when something goes wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.