Better HN
topnewbestaskshowjobs
Log inSign up

shift_reset

3 karma · joined Aug 22, 2019 (7y ago) · 2 submissions

Submissions

comment 7y ago · link · parent

That's what the static keyword means in those array declarators.

    void func(int x[static 10]);
must be called with an argument that is a pointer to the start of a big enough array of int. I can't get recent GCC or Clang to warn on violations of this, though.

comment 7y ago · link · parent

The article has examples of array parameters like

    int b[const 42][24][*]
but you can get more fun with variably modified array parameters instead of just constants like 42. For example,

    double sum_a_weird_shaped_matrix(int n, double array[n][3*n]) {
        double total = 0;
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < 3 * n; ++y) {
                total += array[x][y];
            }
        }
        return total;
    }
has a variable and a more complicated expression in those positions.

But those variably modified parameters can have arbitrary expressions in them, like

    int last(size_t len, int array[restrict static (printf("getting the last element of an array of %zu ints\n", len), len--)]) {
        return array[len];
    }
C++ denies us this particular joy which could have made function overload resolution even more fun.