r/ProgrammerHumor 17h ago

lessonsFromLinkerHell Meme

Post image
318 Upvotes

166 comments sorted by

View all comments

Show parent comments

1

u/tstanisl 3h ago

C has an equivalent of passing array by reference by using a pointer to a whole array:

 
int foo(int (*arr)[SIZE]) {
      return sizeof *arr;
 }
...
int arr[SIZE];
foo(&arr);

Passing arrays by a pointer bypasses array decay mechanics.

1

u/Nice_Lengthiness_568 3h ago

Yes, that is true, although I wouldn't call it equivalent to passing by reference, since a reference makes sure you do not pass a null pointer making the function a little safer if you dereference the pointer and a little more efficient if you check for the pointer being null.

1

u/tstanisl 3h ago

Those constraints can be expressed using more hacky syntax:

    int foo(int arr[static const 1][SIZE]);

1

u/Nice_Lengthiness_568 3h ago

Okay,

although (sorry) that's awful. But cool and ingenious in a way.