Need help understanding what this C code does

Viewed 70

I need help understanding what the following code is.

1 const char* (*srslte_rf_devname)(void* h);

2 int (*srslte_rf_start_rx_stream)(void* h, bool now);

3 int (*srslte_rf_stop_rx_stream)(void* h);

4 void (*srslte_rf_flush_buffer)(void* h);

What I think is happening on line 1 is that the const char* is a pointer that is a constant array of characters and it points to a pointer function called srslte_rf_devname and I dont know what the (void* h) is or what it is doing.

I think line 2 is declaring a pointer function with the name srslte_rf_start_rx_stream and the type is int. I don't know what the (void* h, bool now); is or what it is doing.

I think line 4 is declaring a pointer function with no return value "void" not sure what "type" the function is compared to the other lines.

Please help me to understand what the * means what the (*"some_stuff") means or point me to a resource that explains it.

1 Answers

1. const char* (*srslte_rf_devname)(void *h);

Note that in the C grammar, const char is part of the declaration-specifiers, and * is part of the declarator. So, to understand this grammatically, we should group * with the rest of the declarator, not with the specifiers: const char *(*srslte_rf_devname)(void *h).

Then, what this means is:

  • *(*srslte_rf_devname)(void *h) is a const char.
  • Therefore (*srslte_rf_devname)(void *h) is a pointer to const char.
  • Therefore (*srslte_rf_devname) is a function taking void * and returning a pointer to const char.
  • Therefore srslte_rf_devname is a pointer to a function taking void * and returning a pointer to const char.

After the above, it is easier to see the remaining items:

  • int (*srslte_rf_start_rx_stream)(void *h, bool now); says srslte_rf_start_rx_stream is a pointer to a function taking void * and bool and returning an int.
  • int (*srslte_rf_stop_rx_stream)(void *h); says srslte_rf_stop_rx_stream is a pointer to a function taking void * and returning an int.
  • void (*srslte_rf_flush_buffer)(void *h); says srslte_rf_flush_buffer is a function taking void * and returning void (nothing).
Related