复杂声明

标签 c declaration

我如何解释像这样的复杂声明:

int * (* (*fp1) (int) ) [10]; ---> declaration 1
int *( *( *[5])())(); --------> declaration 2

理解上述声明是否应该遵循任何规则?

最佳答案

这是一篇关于如何在 C 语言中阅读复杂声明的精彩文章:http://www.codeproject.com/KB/cpp/complex_declarations.aspx

对我帮助很大!

尤其是 - 您应该阅读“正确的规则”部分。这里引用:

int * (* (*fp1) (int) ) [10]; This can be interpreted as follows:

  1. Start from the variable name -------------------------- fp1
  2. Nothing to right but ) so go left to find * -------------- is a pointer
  3. Jump out of parentheses and encounter (int) --------- to a function that takes an int as argument
  4. Go left, find * ---------------------------------------- and returns a pointer
  5. Jump put of parentheses, go right and hit [10] -------- to an array of 10
  6. Go left find * ----------------------------------------- pointers to
  7. Go left again, find int -------------------------------- ints.

关于复杂声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1893013/

相关文章:

c - 提高缓存命中率

debian - gets() 没有道歉

c - 在反引号之间执行的程序的 termcap

c++ - 如何理解使用 typedef 定义函数指针?

c - C 中的声明或定义

html - 为了制作出像样的应用程序,我接下来应该阅读什么? (了解 C、HTML 和 MySQL)

c - 如果用户在 C 中输入无效选项,则重复菜单并提示

c++ - constexpr 和带有模板类的静态成员声明

excel - 如何在VBA中使用As声明多个指定类型的变量?