Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Messy function pointer : how to remove the warning?

As I asked and answered in this post. I have the following example code.

#include <stdio.h>

char foo()    { return 'a'; }
char bar()    { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }

char (*gfunclist[])() = {foo, bar, blurga, bletch};

char (*(*x())[])()
{
  static char (*funclist[4])() = {foo, bar, blurga, bletch};
  return funclist;
}

int main() 
{
  printf("%c\n",gfunclist[0]());

  char (**fs)();
  fs = x();
  printf("%c\n",fs[1]()); 
}

My questions are

  • Why the
    return funclist (with "warning: return from incompatible pointer type")
    and
    return &funclist
    both works?
  • I get warning at the line 21 (fs = x();) of
    warning: assignment from incompatible pointer type
    . How to remove this warning?

ADDED

With AndreyT's help. I could get the following code that doesn't have the warnings.

#include <stdio.h>

char foo()    { return 'a'; }
char bar()    { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }

char (*gfunclist[])() = {foo, bar, blurga, bletch};

char (*(*x())[])()
{
  static char (*funclist[4])() = {foo, bar, blurga, bletch};
  return &funclist;
}

int main() 
{
  printf("%c\n",gfunclist[0]());

  char (*(*fs)[4])();
  fs = x();
  printf("%c\n",(*fs)[1]()); 
}

And this is less messy code with the help from peoro.

typedef char (*funptr)();

funptr gfunclist[] = {foo, bar, blurga, bletch};

funptr* x()
{
  static funptr funclist[4] = {foo, bar, blurga, bletch};
  return funclist;
}

int main() 
{
  printf("%c\n",gfunclist[0]());

  funptr *fs;
  fs = x();
  printf("%c\n",fs[1]()); 
}
like image 485
prosseek Avatar asked Feb 25 '23 21:02

prosseek


2 Answers

You have to decide whether you are using C or C++. These languages are significantly different in their treatment of the situations like yours.

In C++ a "pointer to an [] array" (i.e an array of unspecificed size) is a completely different type from a "pointer to an [N] array" (i.e. an array of specified size). This immediately means that your code has no chance to compile as C++ code. It is not a "warning", it is an error. If you want your code to compile as C++, you need to specfiy the exact array size in the function return type

char (*(*x())[4])() // <- see the explicit 4 here?
{
  static char (*funclist[4])() = {foo, bar, blurga, bletch};
  return &funclist;
}

And, of course, you have to return &funclist, since you are declaring your function as returning a pointer to an array.

In main declaring the receiving pointer as char (**fs)() makes no sense whatsoever. The function is returning a pointer to an array, not a pointer to a pointer. You need to declare your fs as

char (*(*fs)[4])(); // <- pointer to an array

i.e. as having pointer-to-array type (note the similarity to the function declaration). And in order to call the function through such a pointer you have to do

printf("%c\n", (*fs)[1]()); 

In C language the explicit array size in the pointer-to-array declarations can be omitted, since in C "pointer to an [] array" type is compatible with "pointer to an [N] array" type, but the other points still stand. However, even in C it might make more sense to specify that size explicitly.


Alternatively, you can stop using pointer-to-array type and instead use pointer-to-pointer type. In that case your function should be defined as follows

char (**x())()
{
  static char (*funclist[4])() = {foo, bar, blurga, bletch};
  return funclist; // <- no `&` here
}

and in main you'll work with it as follows

char (**fs)();
fs = x();
printf("%c\n", fs[1]()); 

Note, that this main is the same as what you had in your original post. In other words, your original code is a bizarre fusion of two different absolutely incompatible techniques. You have to decide which one you want to use and stick to it.

like image 157
AnT Avatar answered Mar 07 '23 23:03

AnT


I'd suggest you to typedef your function pointer, otherwise it's hard to see what's happening.

typedef char (*funptr)();

Anyway, x returns a pointer to char (*(*x())[]), and funclist is not that thing.

Same for fs=x();: char ((())[]) != char (**)();...

If I'm not wrong there are also some errors with precedence between [] and *...

This is working fine:

typedef char (*funptr)();

funptr gfunclist[] = {foo, bar, blurga, bletch};

funptr *x() {
  static funptr funclist[4] = {foo, bar, blurga, bletch};
  return funclist;
}

funptr *fs;
fs = x();
like image 36
peoro Avatar answered Mar 07 '23 22:03

peoro