简体   繁体   中英

Function declaration

int   func(void)   [5]; 

Why is above line not valid in c ? As anyone knows, function can be used as variable. But, I don't understand why compiler gives error .

Why I used that line is because I have tried to create 5 function-variable. To do so, I wrote like that, shown above.

Because it doesn't meet the C language valid syntax?

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.

It is invalid in C++ too because functions cannot be put in arrays (you are trying to declare an array of five functions). However, the following works both in C and 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

int *func(void);

and in C++ you do

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. This makes functions returning array types completely useless.

From the C standard ( n1256 ):





1 A function declarator shall not specify a return type that is a function type or an array type.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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