-1

I’m trying to port a script written in C (in which I have zero knowledge) into JavaScript or PHP. So far, I’ve been OK figuring out most of it, but there’s a function here that eludes me.

In the file https://raw.githubusercontent.com/Stellarium/stellarium/master/src/core/planetsephems/calc_interpolated_elements.c, the “main” function is CalcInterpolatedElements, but then in its parameters we find void (*calc_func)(const double t,double elem[],void *user).

What is this (*calc_func) and what does it do? It seems like, at first, the function CalcInterpolatedElements is called with empty arrays, which are… somewhat (?) populated (?) by (*calc_func) from… I don’t know… Ugh!

Can someone please help?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You should have a look at the code that calls this function, and analyse what is the function provided as a parameter. – Damien Dec 10 '21 at 10:30
  • @Damien: That’s what I tried doing, but not knowing/understanding C, it’s something I can’t figure out. That’s why I asked here… – Pierre Paquette Dec 10 '21 at 22:18

1 Answers1

1
void (*calc_func)(const double t,double elem[],void *user)

calc_func is a pointer to a function that takes three parameters with no return value (void).

I suggest viewing this post on SO.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Chris
  • 26,361
  • 5
  • 21
  • 42
  • I get that, but I can’t see what the function is… What happens to t, elem[], and *user? In other words, what function does it point to? – Pierre Paquette Dec 10 '21 at 06:22
  • That depends on the implementation of the function you pass a pointer to. `calc_func` can point to **_any_** function that takes those parameters and returns `void`. – Chris Dec 10 '21 at 06:24
  • Ouch! I have no clue what that is… That’s why I was asking here… – Pierre Paquette Dec 10 '21 at 22:20
  • Did you review the article I linked to in my answer? – Chris Dec 10 '21 at 23:18
  • Yes, but a) it’s almost Chinese to me, and b) I understand that `calc_func` is a pointer, but it doesn’t tell me—and I can’t figure out—where to… – Pierre Paquette Dec 11 '21 at 00:20
  • 1
    It points to a function. Which function it points to is decided when the function is called. Consider a very simple `add` function `int add(int a, int b) { return a + b; }` Normally we call this like: `add(2, 3)` and get `5`. What if I define a function `apply` that takes two ints and a function pointer as parameters and calls the function being pointed to with those two integers. `int apply(int a, int b, int (*f)(int, int)) { return (*f)(a, b); }` Now I can call: `apply(2, 7, add)` and I get `9`. – Chris Dec 11 '21 at 00:36