This is a 2 part question. To give some background, I have a C code as follows:
int c_func(const char* dir, float a, float b, float c, float d )
{
printf("%s\n", dir);
printf("%f\n",a);
printf("%f\n",b);
printf("%f\n",c);
printf("%f\n",d);
return 0;
}
This is a simple function that takes in a string and 4 floats as arguments and prints them out I am trying to test my phython/C interface. My python code is as follows:
calling_function = ctypes.CDLL("/home/ruven/Documents/Sonar/C interface/Interface.so")
calling_function.c_func("hello",1, 2, 3, 4])
Now since this works, instead of passing 4 individual floats, I would like to pass in a list of 4 floats. I have tried different code online to edit my C function so that it takes in a list as one of its parameters but I cant seem to figure out how to do so as I am a new programmer and I am not experienced with C.
Question 1: How do I code a C function to accept a list as its arguments?
Question 2: This list of four floats is actually coming from a list of lists from my python code. After coding the C function would I be able to use a numpy array called testfv2[0,:] as an input of the C function?testfv2[0,:]is a list of dimensions 1x4 and testfv2 is a list of dimensions 117x4. For now, I would like to into the C function 1 row at a time which is why I thought using testfv2[0,:].
calling_function.c_func("hello",*[1,2,3,4])works with no changes. Note you are using%din your printf. You want%f` for floats. To pass them correctly, set the.argtypese.g.c_func.argtypes = c_char_p,c_float,c_float,c_float,c_float. Right now you are passing them defaultc_intwhich is why%dseems to work.