简体   繁体   English

功能声明

[英]Function declaration

int   func(void)   [5]; 

Why is above line not valid in c ? 为什么以上行在c中无效? As anyone knows, function can be used as variable. 众所周知,函数可以用作变量。 But, I don't understand why compiler gives error . 但是,我不明白为什么编译器会给出error。

Why I used that line is because I have tried to create 5 function-variable. 之所以使用该行,是因为我尝试创建5个函数变量。 To do so, I wrote like that, shown above. 为此,我如上所述写过。

Because it doesn't meet the C language valid syntax? 因为它不符合C语言的有效语法?

May be you should specify what are you trying to do with that sentence in order to get the answer you might be looking for. 可能是你应该指定什么是你想为了得到你可能会寻找答案以这句话做。

This is not legal C syntax, period. 这不是合法的C语法,句号。

It is invalid in C++ too because functions cannot be put in arrays (you are trying to declare an array of five functions). 在C ++中它也是无效的,因为不能将函数放入数组中(您要声明一个包含五个函数的数组)。 However, the following works both in C and C++: 但是,以下内容在C和C ++中均有效:

int (*func[5])();     // C++ version
int (*func[5])(void); // C version

and declares an array of five function pointers . 并声明一个由五个函数指针组成的数组。

If you instead want a function which returns an array, in C you do 如果您想要一个返回数组的函数,请在C中执行

int *func(void);

and in C++ you do 在C ++中

int* func();

or 要么

int (&func())[5];

which returns a reference to an array of five integers. 它返回对五个整数数组的引用

It is trying to declare a function that returns an array. 它试图声明一个返回数组的函数。 This is not allowed - and it's nothing to do with syntax, it's a semantic rule, as demonstrated by the following (which has exactly the same problem, but is obviously syntactically fine): 这是不允许的-与语法无关,它是一个语义规则,如以下所示(具有完全相同的问题,但显然在语法上很好):

typedef int FiveInts[5];
FiveInts func(void);

This is part of the "arrays are special" type rules in C. Function return values are not lvalues, and the only context in which a non-lvalue array could be used is as the subject of the sizeof operator. 这是C语言中“数组是特殊的”类型规则的一部分。函数返回值不是lvalues,唯一可以使用非lvalue数组的上下文是sizeof运算符的主题。 This makes functions returning array types completely useless. 这使得返回数组类型的函数完全无用。

From the C standard ( n1256 ): 从C标准( n1256 ):

6.7.5.3 Function declarators (including prototypes) 6.7.5.3函数声明符(包括原型)

Constraints 约束条件

1 A function declarator shall not specify a return type that is a function type or an array type. 1函数声明器不得指定函数类型或数组类型的返回类型。

Functions cannot return array types or other function types. 函数不能返回数组类型或其他函数类型。 Functions can return pointers to those types, though: 函数可以返回指向这些类型的指针,但是:

int (*func(void))[5];

The syntax is a little weird looking, but it breaks down as follows: 语法看起来有些古怪,但是它分解如下:

      func                 -- func
      func(void)           -- is a function taking no parameters
     *func(void)           -- returning a pointer
    (*func(void))[5]       -- to a 5-element array
int (*func(void))[5]       -- of int

This isn't as useful as it looks: if you try to return a pointer to a local array, such as 这没有看起来那么有用:如果您尝试返回指向本地数组的指针,例如

int (*func(void))[5]
{
  int arr[5] = {0,1,2,3,4};
  return &arr;
}

the array no longer exists when the function returns; 函数返回时,数组不再存在 the pointer value you get back won't point to anything meaningful anymore. 您返回的指针值将不再指向任何有意义的东西。

If you're trying to create an array of functions , you have a similar problem; 如果您尝试创建函数数组 ,则会遇到类似的问题。 you cannot have an array of function types (6.7.5.2, paragraph 1, which includes the sentence "The element type shall not be an incomplete or function type"), although you can have an array of pointers to functions: 您不能具有函数类型的数组(6.7.5.2,第1段,其中包括句子“元素类型不得为不完整或函数类型”),尽管可以具有指向函数的指针数组:

int (*func[5])(void);

This breaks down as 这分解为

      func               -- func
      func[5]            -- is a 5-element array
     *func[5]            -- of pointers
    (*func[5])(void)     -- to functions taking no parameters
int (*func[5])(void)     -- and returning int

Example: 例:

int f0(void) { return 0; }
int f1(void) { return 1; }
int f2(void) { return 2; }
int f3(void) { return 3; }
int f4(void) { return 4; }

int main(void)
{
  int (*func[5])(void) = {f0, f1, f2, f3, f4};
  int i; 

  for (i = 0; i < 5; i++)
    printf("f%d = %d\n", i, (*func[i])());  // or just func[i]()

  return 0;
}

尝试:

int   *func(void);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM