TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

No more confusions on tricky C declarations

132 点作者 ashishb4u将近 15 年前

10 条评论

tptacek将近 15 年前
I've always had a bit more luck with the "typedef each step of the construction" rule-of-thumb. Also, I tend to hide anything as complex as "pointer-to-array-of-pointers-to-functions" (even though you memorize this idiom pretty quickly after an hour in the kernel) behind library ADT's, so you're never indexing an array, but rather passing an index and a whatever_t* to whatever_get(w, index).
评论 #1464423 未加载
评论 #1464714 未加载
评论 #1464320 未加载
评论 #1464884 未加载
loup-vaillant将近 15 年前
An ML like notation would be even more cool:<p><pre><code> char *str[10]; str : [10] (*char) char *(*fp)( int, float *); fp : *((int, *float) -&#62; *char) void (*signal(int, void (*fp)(int)))(int); signal : (int, *(int -&#62; void)) -&#62; *(int -&#62; void) </code></pre> (Oh. That last declaration did make some sense, after all…)<p>Really, how did they manage to chose such an inconsistent, unreadable syntax for their declarations? Is there any rational explanation?
评论 #1465425 未加载
评论 #1464387 未加载
评论 #1464595 未加载
rntz将近 15 年前
Fails on nested arrays.<p><pre><code> char *foo[10][20]; </code></pre> The method described would indicate that this is a array 10 of pointers to array 20s of chars.<p>This is incorrect. It is an array 10 of array 20s of pointers to chars.
评论 #1464669 未加载
jeffmax将近 15 年前
<a href="http://cdecl.org/" rel="nofollow">http://cdecl.org/</a>
评论 #1464274 未加载
评论 #1464247 未加载
Amnon将近 15 年前
I don't see where the spiral comes in. The following rule is simpler:<p>(1) Begin at the variable name, read from left to right, then go back to the name and read from right to left.<p>(2) Give precedence to expressions in parentheses.<p>For example: char <i>(</i>fp)( int, float * )<p>The innermost expression is (* fp). Nothing to the right of the fp. so read to the left: "* ", it's a pointer. Next, we go right and see an arguments list, so it's a pointer to a function taking these arguments. Go back to where we started and read right to left: Pointer to a function taking (int,float *) that returns a pointer to char.
评论 #1464330 未加载
rue将近 15 年前
<p><pre><code> char* str[10]; /* Better */</code></pre>
评论 #1464394 未加载
评论 #1464458 未加载
MtL将近 15 年前
Meh! This is just an overly complication of the right-left rule, which makes you think about "complex" 2D geometry instead of a couple of simple spatial pointers in the declaration you are trying to parse..<p>The easier, more useful version: <a href="http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html" rel="nofollow">http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html</a>
joe_the_user将近 15 年前
It is amazing to me the number of people who would take the time to make ASCII graphics in their replies.<p>I'm blessed that even munging 15+ Linux c/c++ libraries lately I haven't run into anything requiring this - though my Intro to C class, at Merit College twenty five years ago did teach to this rule.<p>Hats off to you anyway...
AndrejM将近 15 年前
No problems here, that is, with D's right-to-left declaration syntax. ;)
评论 #1464239 未加载
ashishb4u将近 15 年前
Sure helps to read code faster!