TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

No more confusions on tricky C declarations

132 pointsby ashishb4ualmost 15 years ago

10 comments

tptacekalmost 15 years ago
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-vaillantalmost 15 years ago
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 未加载
rntzalmost 15 years ago
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 未加载
jeffmaxalmost 15 years ago
<a href="http://cdecl.org/" rel="nofollow">http://cdecl.org/</a>
评论 #1464274 未加载
评论 #1464247 未加载
Amnonalmost 15 years ago
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 未加载
ruealmost 15 years ago
<p><pre><code> char* str[10]; /* Better */</code></pre>
评论 #1464394 未加载
评论 #1464458 未加载
MtLalmost 15 years ago
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_useralmost 15 years ago
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...
AndrejMalmost 15 years ago
No problems here, that is, with D's right-to-left declaration syntax. ;)
评论 #1464239 未加载
ashishb4ualmost 15 years ago
Sure helps to read code faster!