A pointer to a Typedef function, for a function that returns a pointer to a function of its type?

It was a sip. Essentially, I have this:

typedef int (*decker_function_t)(int);

decker_function_t decker;
// Some logic making decker point to a real function
int i = decker(5);

return 1;

However, I really want type functions to decker_function_treturn a type decker_function_t, not int. I cannot figure out how to make typedef self-referential. Is it possible?

Also note that I am asking strictly for C, not C ++,

+4
source share
2 answers

Unable to declare it directly. The language syntax makes it impossible to break the infinite declarative recursion inherent in this declaration, which is very unfortunate because it does not have physical recursion.

Workarounds include

  • "" void (*)(void) . , . , " void *", , void * ( ).

  • . . , " ". ,

    struct fptr_struct {
      struct fptr_struct (*fptr)(int);
    };
    

    , struct fptr_struct. , fptr, .

+3

, SO; , .

void * . , fp :

#include <stdio.h>
typedef int (*dummy_function_type)(int);

typedef dummy_function_type (*decker_function_t)(int);

dummy_function_type some_task(int someparam)
{
  /* do some work.. */
  fprintf(stdout, "%s\n", __FUNCTION__); //gcc specific
  return 0;
}

dummy_function_type decker_queue_root(int redundantparam)
{
  /* do some work.. */
  fprintf(stdout, "%s\n", __FUNCTION__); //gcc specific
  return (dummy_function_type)some_task;
}

void process_decker_queue(decker_function_t jobs)
{
  /* nonstandard gcc function nesting follows */
  decker_function_t next_decker(decker_function_t cur_decker, int param)
  {
    if(!cur_decker)return 0;
    return (decker_function_t)(cur_decker(param));
  }

  decker_function_t cursor;
  cursor = jobs;
  while(cursor)
  {
    cursor = next_decker(cursor, 0); //param seems pretty redundant in this context..
  }
}

int main()
{
  //work queue stack
  decker_function_t prime = &decker_queue_root;
  process_decker_queue(prime);
  fprintf(stdout, "this compiles..\n");
  return 0;
}

:

$ ./test_frecurse 
decker_queue_root
some_task
this compiles..

+1

All Articles