5 comments

  • pcfwik33 minutes ago
    Being taught this rule in undergrad really hampered my appreciation of C. As I&#x27;ve said in a previous comment, the real key that unlocked understanding C declarations for me is the mantra &quot;declaration follows use.&quot; You declare a variable in C <i>in exactly the same way you would use it:</i> if you know how to use a variable, then you know how to read and write a declaration for it. Once I understood this elegant idea, it became hard to enjoy using statically typed languages that eschew it.<p>It is explained in more detail at this link: <a href="https:&#x2F;&#x2F;eigenstate.org&#x2F;notes&#x2F;c-decl" rel="nofollow">https:&#x2F;&#x2F;eigenstate.org&#x2F;notes&#x2F;c-decl</a>
  • Joker_vD37 minutes ago
    &gt; &quot;str is an array 10 of pointers to char&quot;<p>Wow, imagine if it was possible to actually use a language like that do declare the type of the variable like that? Something like<p><pre><code> str: array [0..9] of ^Char; </code></pre> or even<p><pre><code> var str [10]*uint8 </code></pre> Just imagine...
  • stephencanon38 minutes ago
    This comes up every so often, and while it&#x27;s sort of almost true and attractive, it isn&#x27;t actually correct.<p>The correct rule is &quot;follow the C grammar&quot;. An easier to remember and also correct rule is &quot;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...&quot; (this is sometimes called the &quot;right-left rule&quot;: <a href="https:&#x2F;&#x2F;cseweb.ucsd.edu&#x2F;~gbournou&#x2F;CSE131&#x2F;rt_lt.rule.html" rel="nofollow">https:&#x2F;&#x2F;cseweb.ucsd.edu&#x2F;~gbournou&#x2F;CSE131&#x2F;rt_lt.rule.html</a>).
    • jcranmer24 minutes ago
      The best summary--and easiest to remember, IMHO--is that variables are declared as they are used.<p>Want to write an array of function pointers that return a pointer to an array of pointers to int? Well, that&#x27;s:<p><pre><code> array ... -&gt; x[N] ... of function pointers ... -&gt; T (*x[N])() ... that return a pointer ... -&gt; T *(*x[N])() ... to an array ... -&gt; T (*(*x[N])())[M] ... of pointers ... -&gt; T *(*(*x[N])())[M] ... to int ... -&gt; int *(*(*x[N])())[M] </code></pre> It doesn&#x27;t make it all that easy to read, but you can at least write the complex types pretty reliably.<p>(The real answer is of course to just typedef every function pointer type or pointer-to-array and not worry about it anymore.)
    • mananaysiempre24 minutes ago
      Why would you do that? Ignoring for the moment arguments of prototypes and sizes of arrays, read C declarations the way they were designed: “char *a[&lt;whatever&gt;]” means that the expression *a[&lt;whatever&gt;] has type char; “char (*a(&lt;whatever&gt;))[&lt;whatever&gt;]” means that the expression (*a(&lt;whatever&gt;))[&lt;whatever&gt;] has type char; and so on. Then you apply the normal precedence rules for expressions, and in this case only knowing them for the prefix and postfix operators is sufficient. (Hint: all prefix operators have one precedence and all postfix ones another, and you know which one binds tighter if you know what *i++ means.)
  • classified29 minutes ago
    This is useful if you&#x27;re far from the internet. Here&#x27;s a web site that translates the gibberish to English or vice versa:<p><a href="https:&#x2F;&#x2F;cdecl.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cdecl.org&#x2F;</a><p>Or<p><pre><code> apt install cdecl </code></pre> on Linux.
  • AlienRobot32 minutes ago
    This is why I like python.<p>str is a duck.