1

There's anyway to define functions inside an array in C? I don't want to create all those functions because it will polute my code and there's no need for this. I was thinking something like this:

double (q[1])(X[2])={(double X[2]){return X[0]}};

but it seems not right way to do that.

I will be clear, i have alot of derivatives, but i now their formula and it will be used in few places in my code, so, if i create a function to each derivative would make my code unreadble, the best solution would be create them inside a vector, and call them as a array of functions.

  • It is not what i want, i would need to declare those functions in my code and put them into an array. I wish to declare them inside an array without need to create in another place. – João Paulo Andrade Aug 06 '18 at 02:58
  • 5
    You won’t be able to do so. The closest thing you can do is to use function pointers. The function would have to be declared elsewhere. – Logan Aug 06 '18 at 02:59
  • 4
    C does not work that way. – Christian Gibbons Aug 06 '18 at 02:59
  • 1
    I don't even understand what `functions inside an array` mean and what you want with that weird syntax. Array elements store data and not code. It can store function pointers though. If you have a lot of similar function implementations then use a macro instead – phuclv Aug 06 '18 at 04:09

0 Answers0