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]());
}
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 typeAnd, of course, you have to return
&funclist, since you are declaring your function as returning a pointer to an array.In
maindeclaring the receiving pointer aschar (**fs)()makes no sense whatsoever. The function is returning a pointer to an array, not a pointer to a pointer. You need to declare yourfsasi.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
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
and in
mainyou’ll work with it as followsNote, that this
mainis 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.