开发者

Messy function pointer : how to remove the warning?

开发者 https://www.devze.com 2023-01-23 11:54 出处:网络
As I asked and answered in this post. I have the following example code. #include <stdio.h> char foo(){ return \'a\'; }

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 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.


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();


For the first question,

cdecl> explain char (*(*x())[])()
declare x as function returning pointer to array of pointer to function returning char
cdecl> explain static char (*funclist[4])()
declare funclist as static array 4 of pointer to function returning char

So the expected return type of x is a pointer to an array of pointers to functions returning char, but what you're returning is just an array of pointers to functions returning char. By adding the &, you are now in fact returning a pointer to the array of pointers to functions returning char.

For the second, again the expected return type of x is a pointer to an array of pointers to functions returning char, but we see

cdecl> explain char (**fs)();
declare fs as pointer to pointer to function returning char

What you want instead is char (*(*fs)[])();

cdecl> explain char (*(*fs)[])();
declare fs as pointer to array of pointer to function returning char


Choose either the extra * or the [] in the return type of x, not both.


When you do T a[] = { .. }; then a is declared as having type T[N], but not as having type T[].

So you need to put some number in the brackets.

char (*(*x())[sizeof(gfunclist)/sizeof(*gfunclist)])()
{
  return &gfunclist;
}

It will limit what you can return to a specific size. This is where C++ differs from C, which will allow you to assign a T(*)[N] to an T(*)[]. C++ has no such so-called "type compatibility" rules. If you want to get rid of the need for the &, you need to return the decay-type of the array. The element type of your array is char(*)()

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

Using an identity template you can make your declaration look much more readable

template<typename T> struct identity { typedef T type; };
identity<char()>::type **x()
{
  static identity<char()>::type *funclist[] = {foo, bar, blurga, bletch};
  return funclist;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消