I have read about syntax of function pointers and I can't make to work in my code.
This video explains that the syntax is:
type func0(type0 arg0, ..., typen argn){
// do something
return a_thing;
}
Then for passing function pointers in another function is:
type func1((*func0)(type0 arg0, ..., typen argn), another args){
//do something
func0(arg0, ..., argn);
return a_thing;
}
But then, I read the first answer about How do function pointers in C work?, and the syntax is:
type func1((*func0)(type0 arg0, ..., typen argn), another args) {
//do something
(*func0)(arg0, ..., argn);
return a_thing;
}
which makes sense because you are dereferencing the pointer, which you are passing. Lastly, the first answer in C function pointers invocation syntax explain that the syntax can be:
&func
func
*func
**func
and I don't understand which info is correct. I have the code:
double *func1((*func0)(double), double *var) {
double *temp = malloc(some_size);
for(int i = 0; i < some_cycles; ++i) {
temp[i] = (*func0)(var[i]);
}
return temp;
}
And it throws the error a value of type "double *" cannot be assigned to an entity of type "double"
EDIT:
For anyone wanted to see the real code, was that:
tensor_t *applyBinOpTensor(double *(operation)(double, double), tensor_t *tensor, double operand){
size_t total_size = indiceProduct(*tensor->shape);
double *values = malloc(total_size * sizeof(*values));
for(size_t i = 0; i < total_size; ++i){
values[i] = (*operation)(tensor->values[i], operand);
}
return initializeTensor(values, tensor->shape);
}
As you can see, all the problem was that I typed double *(operation)(double, double) instead of double (*operation)(double, double).
Thanks to all for your answers. I learned a lot, and sorry for my misspeling.
And thorw the errorthe code you presented is missing}}and#include <stdlib.h>. Could you please post real full code example?{in the wierdest places, so it is impossible to be sure what you mean. And please indent your code correctly.