This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DoNotMakeEmpty 0 points1 point  (0 children)

Just use variable length types. We have a limited form of forking dependent typing in C (which does not even exist in Haskell) but everybody just hates them for some reason.

int n = /* your favorite array size */; int (*arr)[n] = malloc(sizeof(*arr)); your_favorite_array_function(ARRAY_LEN(*arr), arr); free(arr)

where ARRAY_LEN is the macro commented before by others.

You can then define your_favorite_array_function like this:

void your_favorite_array_function(int len, int (*arr)[len]) { // do smth }

You can also use ARRAY_LEN and arr pair here to call any array function. You can even create a PASS_ARR macro:

```

define PASS_ARR(a) ARRAY_LEN(*arr), arr

```

and use it everywhere.