C array types are weird

(anselmschueler.com)

44 points | by signa11 1 day ago

6 comments

  • kazinator 5 minutes ago
    [delayed]
  • uecker 1 day ago
    In practice, the [static n] notation can give you useful warnings and bounds checking.

    https://godbolt.org/z/PzcjW4zKK

    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: https://godbolt.org/z/ao1so9KP7

    • keyle 1 hour ago
      I know of this notations but I don't see many people using [static n].

      Not sure why, maybe it doesn't feel like C anymore, maybe it feels hacky?

      typically if you're passed an array you'd want to get more anyway, so you'd get passed a struct. Not sure.

    • dnautics 2 hours ago
      What is **int[3][5]
      • thrance 40 minutes ago
        A pointer to a pointer to a pointer to a pointer of integers.
      • ori_b 1 hour ago
        A syntax error. You need a variable name, not a type name, in the middle.
        • fusslo 38 minutes ago
          or a rejected PR
        • ori_b 1 hour ago
          And if you want 'int **a[a][b]', it's a value that when you say 'x = **a[m][n]', will evaluate to an int and assign it to x. Postfix has higher precedence than prefix.
  • the__alchemist 1 hour 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's &[] array syntax. Arrays/lists/Vecs etc in most non-C languages feel like an abstraction over a collection of items; I feel like C's exposing the pointer directly is taking a low-level memory/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/lists/vecs for higher level operations on collections.

    Tangent: I have a pet theory that part of Zig's raison d'etre is to fix some of the problems with C, while accommodating its pointer-based data structures, and the resulting patterns.

  • fatty_patty89 2 hours ago
    there's no array type in c
    • colejohnson66 1 hour ago
      Yes it does. It just decays to a pointer at the slightest touch.
      • throwaway27448 1 hour ago
        So why are we discussing it
        • dwattttt 39 minutes ago
          Because doing a dance to avoid it decaying conveys better information to both the compiler and downstream users of your code.
  • IncreasePosts 2 hours ago
    Paging walter bright
    • glouwbug 2 hours ago
      C's biggest mistake.

      But in other news most don't know that a[3] == 3[a]

  • throwaway27448 1 hour ago
    Why are we still discussing c in 2026? Why are you intentionally hamstringing yourself unless you're using fucking hp-ux
    • smackeyacky 26 minutes 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
    • cartoonfoxes 53 minutes ago
      • mianos 49 minutes ago
        p.s. in case you don't want to follow the link, number 2 on the list