Comment by pjmlp
25 days ago
Proper strings and arrays for starters, instead of being pointers that the programmer is responsible for doing length housekeeping.
25 days ago
Proper strings and arrays for starters, instead of being pointers that the programmer is responsible for doing length housekeeping.
Arrays are not pointers and if you do not let them decay to one, they do preserve the length information.
They surely behave like one as soon as they leave local scope.
Kind of hard when passing them around as funcion parameters, and the static trick doesn't really work in a portable way.
Lets seen how far WG14 gets with cybersecurity laws with this kind of answers being analysed by SecDevOps and Infosec experts.
Then don't allow it to decay:
Using gcc (and similarly clang) removing the '15' from 'array', and allowing it to allocate it as 14 chars will result in warnings for both function calls.
One can hide that ptr to array behind a typedef to make it more readable:
What do you mean by 'the static trick'? Is that what I have in sptr_fn()?
3 replies →
We can look at this code like it passes an array by reference, but how to pass `arr` by value?
You can pass it by value when putting it into a struct. You can also pass a pointer to the array instead of letting it decay.
void foo(int (*arr)[4]);
int arr[4]; foo(&arr);