CYOA, C language and Warning poem

This is not a programming forum, so if my question isn’t relevant, throw me out.
For a CYOA in C, I need a page() function, which returns a function of the same type. The easiest way is to use a void pointer on a function, but the warnings start Vogon poetry (with the option -pedantic for gcc):

to compile: gcc -Wall -W -pedantic -std=c99 test.c -otest

#include <stdio.h>
typedef void* page;
page page1(void);
page page2(void);

page page1(void)
{
  printf("page1\n");
  return page2;
}

page page2(void)
{
  printf("page2\n");
  return page1;
}

int main(void)
{	
   page (*pf)(void);
   page (*pt)(void);
   pf = page1;
   pt = pf();
   pt();
   pf = pt;
   return 0;
}

I changed it to the following code, but I still have a warning on pt = pf();

#include <stdio.h>
typedef void (*page)(void);
page page1(void);
page page2(void);

page page1(void)
{
  printf("page1\n");
  return (page)page2;
}

page page2(void)
{
  printf("page2\n");
  return (page)page1;
}

int main(void)
{	
   page (*pf)(void);
   page (*pt)(void);
   pf = page1;
   pt = (page (*)()) pf();
   pt();
   pf = pt;
   return 0;
}

update:
Replacement of pt = pf(); by pt = (page (*)()) pf(); in the main function.

1 Like

can’t help you with your code, but what’s the reason for going with C instead of a tailor made option?

Why do you use a void pointer as page? Using a function pointer seems more natural (to me).

This is in the C FAQ: http://c-faq.com/decl/recurfuncp.html

C doesn’t permit this type declaration, but there are a couple of workarounds.

1 Like

I’m not sure I understand what “a tailor made option” means, but the C language is really nice and I want to target the MS-DOS operating system.

How do you do it?

This is exactly the answer to the question I was asking myself, and I couldn’t find it on the internet. All the examples I found never addressed the issue head-on. I’ll look at it; thank you.

Here you declare that page is a pointer to a function that returns void. But your functions don’t return void.

Of the two examples in the FAQ, the second is probably the easiest to wrap one’s head around. Represent the page by a struct, with a function pointer inside.

Yes, but now if I replace in my second example, and in the main() function, pt=pf() by pt = (page (*)()) pf(); I have no more warnings!
Don’t ask me why, this is the kind of little exercise to solve, technical and intellectual, that you must like but that I don’t want to have to do! I’ve tried so many things I don’t understand it anymore.

My original thinking was quite close to your second example (which I had misread).

No more warnings for GCC and turbo C.