Hacker News new | ask | show | jobs
by CuriousCosmic 585 days ago
the function signature certainly should be type def-ed. i.e.

    typedef char (*fn())[5];
and then you have the original as

    fn x [3];
1 comments

I don't keep up with the latest C shenanigans, cuz I like C the way it was, but have they change something where "pointer to an array[5]" is a meaningful distinction to draw?

I mean, "a pointer to an array of X" is simply "a pointer to X" and using hungarian notation, you can encode the knowledge "this pointer can be incremented" into its name.

and typedef'ing *function declarations? who has families of functions with the same type signatures that they want to point to?

> but have they change something where "pointer to an array[5]" is a meaningful distinction to draw

This has been a meaningful distinction since at least C99.

> "a pointer to an array of X" is simply "a pointer to X"

They aren't actually the same. The former can be decayed into the latter but they aren't actually equivalent and you'll get type mismatches if you try to treat them as the same thing.

> who has families of functions with the same type signatures that they want to point to

An example is callbacks or handlers for responding to interrupt requests. A lot of hardware interface code relies on typedef-ed function decls because very often you are passing user side functions through your interface so that you can stash those function pointers somewhere and invoke them when some event occurs.

> "a pointer to an array of X" is simply "a pointer to X"

I don't believe these two are the same. An "array of X" indeed decays to a "pointer to X". But a "pointer to an array of X" is something else. E.g.

    int foo[3];  // array of int
    int *foo;    // pointer to int
    int *foo[3]; // pointer to array of int
Perhaps the first two are what you mean, though, and this is just a terminology issue.

  int *foo[3]; // pointer to array of int
that's an array of 3 pointers to ints. if you pass foo as an argument you get a pointer to a pointer to an int (with knowledge if you can hang onto it that there are more pointers to ints lined up in memory)
Yes, but this is different:

  int (* foo) [3]
This is a pointer to an array of 3 ints.

And you could pass this to an appropriate function as an argument, to pass the whole array, not just a decayed pointer.

And more generally, I'd group things as unambiguously as possible even in your example:

    int * (foo [3])
to make the intent clearer