10 comments

  • alexey-salmin16 minutes ago
    Interestingly the article doesn&#x27;t mention two-dimensional arrays and they&#x27;re curios because they bring a certain asymmetry with them. It always tripped me over the most in C because I otherwise find the language very &quot;symmetrical&quot;. It often feels like in design of this language the beauty of expressing certain things took priority over readability or safety which I admire in a way. But somehow not in the case of the two-dimensional arrays.<p>If you see a[i][j] it could mean two completely different things:<p>1) &quot;a&quot; is a continuous chunk of memory of N*M bytes, so it behaves as char*; a[i][j] == *(a + i*M + j)<p>2) &quot;a&quot; is an array of char* pointers that point to N completely distinct memory chunks of size M, so it behaves as char**; a[i][j] == *(*(a + i) + j)<p>With flat arrays the difference between an array as a variable and a pointer to the first element is literally negligible because you won&#x27;t even see the difference in the assembly. This is why the automatic decay-to-pointer makes a lot of sense.<p>But that breaks completely with multiple dimensions. You definitely see the difference in the assembly because the memory layout is so different.
  • fooker2 hours ago
    C array types are weird because C doesn&#x27;t really need arrays. It&#x27;s not what C was about.<p>But if you designed a language in the era where Fortran, THE array language, reigned supreme, nobody would use your language. The mindshare Fortran had is difficult to convey now, half a century later.<p>Think of it like making a chatbot today and not mentioning AI or LLMs, that&#x27;s what making a language without arrays would have felt like in 1970.
    • alexey-salmin5 minutes ago
      People who do HPC in C actually wish C had proper arrays like Fortran. If your function takes two pointers as inputs instead of two arrays they can alias the same memory and in fact they may alias any other pointer of the same type. Writing into one of them invalidates all the values you have in registers so you have to load them again.<p>The &quot;restrict&quot; keyword was invented to solve this but it still has weaker semantics than original Fortran arrays. It can still solve a big share of problems, but it never got proper adoption and never even made it into C++.
  • uecker1 day ago
    In practice, the [static n] notation can give you useful warnings and bounds checking.<p><a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;PzcjW4zKK" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;PzcjW4zKK</a><p>And while the (*array_ptr)[3] notation take a moment to get used to, it is very logical. If you have a pointer to an array, you dereference it first and then indx into it. Again, useful for bounds checking: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;ao1so9KP7" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;ao1so9KP7</a>
    • kazinator44 minutes ago
      The parentheses in (*parray)[i] would be unnecessary if dereferencing used postfix notation.<p><pre><code> Current: All postfix *ptr[3] ptr[3]* &#x2F;&#x2F; indexed access, then deref (*ptr)[3] ptr*[3] &#x2F;&#x2F; deref, then indexed access*</code></pre>
    • keyle4 hours ago
      I know of this notations but I don&#x27;t see many people using [static n].<p>Not sure why, maybe it doesn&#x27;t feel like C anymore, maybe it feels hacky?<p>typically if you&#x27;re passed an array you&#x27;d want to get more anyway, so you&#x27;d get passed a struct. Not sure.
    • dnautics5 hours ago
      What is **int[3][5]
      • kazinator34 minutes ago
        In C declaration syntax, there is a &quot;stem&quot; called <i>declaration specifiers</i> consisting of specifiers and qualifiers. That&#x27;s where <i>int</i> can appear. After that, there is a declarator. In some cases, multiple declarators separated by a comma, which share the same &quot;stem&quot;.<p><pre><code> int a, b, *c; &#x2F;&#x2F; one stem consisting of &quot;int&quot;, three declarators. </code></pre> The * is declarator syntax for deriving a pointer type. It never appears such that a type specifier would come after it somewhere to the right.<p>Some languages have extended the C declaration syntax such that the type derivators can be moved from the declarator part to the &quot;stem&quot;. For instance, as an alternative to:<p><pre><code> int a[10]; </code></pre> you can write<p><pre><code> int[10] a; </code></pre> This is how we could get<p><pre><code> **int[3] </code></pre> as a declarator stem indicating an array of 3 pointers to pointers to int. But it&#x27;s not in C.
      • ori_b4 hours ago
        A syntax error. You need a variable name, not a type name, in the middle.
        • ori_b4 hours ago
          And if you want &#x27;int **arr[a][b]&#x27;, it&#x27;s a value that when you say &#x27;x = **arr[m][n]&#x27;, will evaluate to an int and assign it to x. Postfix has higher precedence than prefix.
        • fusslo3 hours ago
          or a rejected PR
      • thrance3 hours ago
        A pointer to a pointer to a pointer to a pointer of integers.
  • Animats42 minutes ago
    The real lack is that C doesn&#x27;t have slices. Slices can do most of what pointers into arrays can do, with sane semantics. Slices were invented surprisingly late. They were implementable in the 1970s, but didn&#x27;t really show up until the 1990s. Now that we have slices, the demand for pointers into the middle of an array has much decreased.<p>I had a go at retrofitting C with slices over a decade ago.[1] Too much political hassle.<p>[1] <a href="https:&#x2F;&#x2F;www.animats.com&#x2F;papers&#x2F;languages&#x2F;safearraysforc43.pdf" rel="nofollow">https:&#x2F;&#x2F;www.animats.com&#x2F;papers&#x2F;languages&#x2F;safearraysforc43.pd...</a>
  • kazinator3 hours ago
    There is a history to it; in one of the predecessor languages, like B, Ritchie actually had arrays that had a hidden pointer to their start. The &quot;array to pointer decay&quot; was actually a real operation that loaded an address from memory, and it was possible to twiddle the bits to relocate an array. One problem with it was no way to initialize such a pointer field that would allow an array to live in dynamically allocated storage (no constructors in the language).<p>So in short, the bad design (array values produce pointers) was informed by conceptual compability with an earlier design in which that was literally happening.
    • xenadu0217 minutes ago
      Not just this it is important to remember that there was no &quot;aha!&quot; moment where C was created whole-cloth by writing the first compiler in B then cross-compiling.<p>The language B was evolved in-place by adding new features, then editing the compiler source to make use of those new features, then repeating. They simply started calling it &quot;New B&quot;. At some point the language had evolved sufficiently that they decided to call it C.<p>The semantics of arrays were inherited from B and simply never changed. Part of me suspects this was also because it was seen as &quot;clever&quot; at the time. Look ma, we let arrays turn into pointers! Isn&#x27;t that clever?<p>When you look at pre-ANSI C function prototypes you wonder &quot;where are the parameter types?&quot; because there are none. The compiler didn&#x27;t bother to check. Part of that was perhaps for implementation reasons but a big part of that was the feeling or culture inherited from B: in that language you just had words of memory. You were free to interpret any word of memory as any data type you liked. So duh of course it is up to you to decide how many parameters your function received and of what type. If the caller supplied a different number or different types? Don&#x27;t do that.<p>If you are coming from that sort of world clever tricks like arrays decaying to pointers or automatically converting between data types and sizes seems perfectly natural. Anything C offers above and beyond that is an improvement from B after all.
  • the__alchemist4 hours ago
    This is one of the things that I feel is an inappropriate abstraction that is around for historical reasons. When I do FFI to call C from rust, I usually wrap the generated API (Which is pointer based) into rust&#x27;s &amp;[] array syntax. Arrays&#x2F;lists&#x2F;Vecs etc in most non-C languages feel like an abstraction over a collection of items; I feel like C&#x27;s exposing the pointer directly is taking a low-level memory&#x2F;MMIO operation and inserting it into business logic. Conceptually, I like to keep them separate; pointers for writing drivers, accessing registers, writing to flash memory etc. Arrays&#x2F;lists&#x2F;vecs for higher level operations on collections.<p>Tangent: I have a pet theory that part of Zig&#x27;s raison d&#x27;etre is to fix some of the problems with C, while accommodating its pointer-based data structures, and the resulting patterns.
    • vulcan014 hours ago
      This talk – &quot;Programming without pointers&quot; – by Andrew Kelley may be interesting to you.<p><a href="https:&#x2F;&#x2F;www.hytradboi.com&#x2F;2025&#x2F;05c72e39-c07e-41bc-ac40-85e8308f2917-programming-without-pointers" rel="nofollow">https:&#x2F;&#x2F;www.hytradboi.com&#x2F;2025&#x2F;05c72e39-c07e-41bc-ac40-85e83...</a>
      • throwaway274484 hours ago
        Learning to program with pointers is enormously useful. It&#x27;s simply bad software engineering to not use typing to enforce constraints on access to pointers (or addresses, or however you&#x27;d like to term them)
        • doyougnu1 hour ago
          IIRC that talk of about using indices (u32) to represent data in an array. That is orthogonal to representing that information in the type system since you can just type the index
  • mlmonkey1 hour ago
    It still cracks me up that 3[x] and x[3] mean the same thing in C.
  • IncreasePosts5 hours ago
    Paging walter bright
    • WalterBright1 hour ago
      At your service! D fixed it, and I&#x27;m sorry C users have suffered as the array-to-pointer decay blasted their kingdom. Fixing it in C is easy and should be the #1 priority.
    • glouwbug5 hours ago
      C&#x27;s biggest mistake.<p>But in other news most don&#x27;t know that a[3] == 3[a]
      • glouwbug1 hour ago
        <a href="https:&#x2F;&#x2F;www.digitalmars.com&#x2F;articles&#x2F;C-biggest-mistake.html" rel="nofollow">https:&#x2F;&#x2F;www.digitalmars.com&#x2F;articles&#x2F;C-biggest-mistake.html</a>
      • parlortricks1 hour ago
        I didn&#x27;t understand why a[3] == 3[a], but i found this stackoverflow that explains it.<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;16163840" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;16163840</a><p><i>In C a[i] is converted to *(a+i) internally. i[a] is converted to *(i+a). Array names also act as pointers in c. so (a+i) or (i+a) give an address (using pointer arithmetic) that is dereferenced using</i>
      • throwaway274484 hours ago
        Even more irrelevant than the array type
  • fatty_patty895 hours ago
    there&#x27;s no array type in c
    • colejohnson664 hours ago
      Yes it does. It just decays to a pointer at the slightest touch.
      • throwaway274484 hours ago
        So why are we discussing it
        • dwattttt3 hours ago
          Because doing a dance to avoid it decaying conveys better information to both the compiler and downstream users of your code.
  • throwaway274484 hours ago
    Why are we still discussing c in 2026? Why are you intentionally hamstringing yourself unless you&#x27;re using fucking hp-ux
    • smackeyacky3 hours ago
      Embedded programming is still in C for a lot of micro controllers and whatnot. If you’re programming with limited resources it’s essential to understand pointers and arrays. Likely you won’t be doing anything useful without them
    • cartoonfoxes3 hours ago
      <a href="https:&#x2F;&#x2F;www.tiobe.com&#x2F;tiobe-index&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.tiobe.com&#x2F;tiobe-index&#x2F;</a>
      • pdpi2 hours ago
        As always, the TIOBE Index is of dubious value. The fact that it ranks Delphi above both Go and Rust should give you an idea of why.
        • keyle2 hours ago
          Yeah it&#x27;s rather odd, also jumps like this in ADA makes you wonder<p><a href="https:&#x2F;&#x2F;www.tiobe.com&#x2F;tiobe-index&#x2F;ada&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.tiobe.com&#x2F;tiobe-index&#x2F;ada&#x2F;</a>
      • mianos3 hours ago
        p.s. in case you don&#x27;t want to follow the link, number 2 on the list