编程与软件工程c-faq.com
C 声明的顺时针/螺旋规则
The Clockwise/Spiral Rule of C declarations
2010 — 2016,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 C FAQ 网站上的一篇经典文章,介绍解读 C 复杂声明(如指向函数指针数组的指针)的“顺时针/螺旋法则”:从标识符出发,沿螺旋方向交替阅读数组和指针符号,从而弄清声明的真实含义。
为什么经久不衰:C 的声明语法以晦涩著称,这篇文章给出的记忆技巧多年来反复被推荐,是理解 C 类型声明绕不开的入门读物。
HN 评论精选
螺旋法则不严谨,应改用更准确的“右左法则”
螺旋法则其实是胡扯。正确的做法是遵循 C 语法。一个更好记且同样正确的规则是“右左法则”:从被声明的标识符出发向外读,先向右读到闭括号,再向左回到对应的开括号,然后继续向右……螺旋法则只是绕着真相打转,不够精确,没有实用价值。
stephencanon查看英文原文
This has been posted before[1], and the "spiral rule" is a load of hooey. The correct rule is "follow the C grammar". An easier to remember and also correct rule is "start at the identifier being declared; work outwards from that point, reading right until you hit a closing parenthesis, then left until you hit the corresponding open parenthesis, then resume reading right..." (this is sometimes called the "right-left rule"[2]). The "spiral rule" dances around the truth without actually being precise enough to be useful. [1] https://news.ycombinator.com/item?id=5079787 [2] http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
把声明当作表达式逐层求值来理解类型
更简单的方法:把类型的每个子部分当作表达式由内向外读,剩下的类型就是该表达式的类型。例如对 const char *foo[][50],foo 是 const char* 的二维数组,foo[0][0] 是 const char*,解引用后得到 const char。
colanderman查看英文原文
Way simpler: from inside out, read any subpart of the type as an expression. (Arrays have precedence over pointers, as usual.) The type that remains is that expression's type. So e.g. given the type: const char *foo[][50] the following expressions have the following types: foo -> const char *[][50] foo[0] -> const char * [50] foo[0][0] -> const char * *foo[0][0] -> const char Another example: int (*const bar)[restrict] bar -> int (*const)[restrict] *bar -> int [restrict] (*bar)[0] -> int One more: int (*(*f)(int))(void) f -> int (*(*)(int))(void) (*f) -> int (* (int))(void) (*f)(0) -> int (* )(void) *(*f)(0) -> int (void) (*(*f)(0))() -> int (In this case, the last expression is more readily written as f(0)(), since pointers to functions and functions are called using the same syntax.)
用运算符优先级和“声明跟随使用”原则解释声明读法
真正的规则是:类型构造运算符模仿一元和后缀运算符(声明跟随使用)。后缀 [] 和 () 优先级高于一元 *,且后缀左结合、一元右结合。按优先级和结合性从内向外读,走出来的路径确实是个螺旋,但不必纠结螺旋这个说法。
kazinator查看英文原文
The real rule is that the type construction operators mirror the unary and postfix family of operators (declaration follows use). For instance unary * declares a pointer, mimicking the dereference operator, and postfix [] and () declare functions, mimicking array indexing and function call. To follow the declaration you make use of the fact that postfix operators in have a higher precedence than unary, and that of course unary operators are right-associative, whereas postfix are left-associative (necessarily so, since both have to "bind" with their operand). So given int ***p[3][4][5]; we follow the higher precedence, in right to left associativity: [3], [4], [5]. Then we run out of that, and follow the lower-precedence * * * in right-to-left order. If there are parentheses present, they split this process. We go through the postfixes, and then the unaries within the parens. Then we do the same outside those parens (perhaps inside the next level of parens): int ****(***p[3][4][5])[6][7]; 1 2 3 4 765 8 9 1111 3210 Start at p, follow postfixes, then unaries within parens. Then the postfixes outside the parens and remaining unaries. The result is in fact a spiral just from going root postfix unary out postfix unary out. We just don't have to focus on the spiral aspect of it.
螺旋法则在嵌套数组声明上会给出错误结果
这个方法在嵌套数组上会出错。对 char *foo[10][20],按文中方法会读成“10 个指向 20 个 char 的数组的指针的数组”,但正确含义是“10 个数组的数组,每个元素是指向 char 的指针”。
rntz查看英文原文
Fails on nested arrays. char *foo[10][20]; The method described would indicate that this is a array 10 of pointers to array 20s of chars. This is incorrect. It is an array 10 of array 20s of pointers to chars.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选