2014-03-13 115 views
-2
int *(*const fun[])(int argc, char **argv) 

这两个函数指针声明有什么区别?

const int *(* fun[])(int argc, char **argv). 

是第一个常量函数指针返回整数指针的数组?

+0

当你问'cdecl',那有什么告诉你吗? –

+0

[cdecl](http://cdecl.org/)为它们提供了'语法错误'。然而GCC不会抱怨...... – Kninnug

+1

对于cdecl来解析它,你需要删除参数名,即使它成为“int *(* const fun [])(int,char **)”。 –

回答

2

第一个是只读的指针数组(即不能更改fun[i])到功能接收intchar **,并返回一个指针int

第二个是非常相似,除了你可以改变fun[i],但它指向的函数返回一个指向只读整数的指针。

因此,简而言之:

/* First declaration 
int *(*const fun[])(int argc, char **argv) 
*/ 
int arg1; 
char **arg2; 
int *example = (*fun[i])(arg1, arg2); 
*example = 14; /* OK */ 
example = &arg1; /* OK */ 
fun[i] = anoter_function; /* INVALID - fun[] is an array of read-only pointers */ 

/* Second declaration 
const int *(* fun[])(int argc, char **argv) 
*/ 
const int *example2 = (*fun[i])(arg1, arg2); 
fun[i] = another_function; /* OK */ 
*example2 = 14; /* INVALID - fun[i] returns pointer to read-only value. */ 
example2 = &arg1; /* OK */ 
+0

应该将'example2'设为'const'? – immibis

+0

@immibis绝对。感谢您指出,我解决了它。 –