3 ms·
The proper simple syntax IMHO should better be something like: void foo(int ptr[n], size_t n); with ptr[n] not copying the full array, just that n is the
by rurban 2y ago
The proper simple syntax IMHO should better be something like:
void foo(int ptr[n], size_t n);
with ptr[n] not copying the full array, just that n is the size.
you can try it now with:
#include <stddef.h>
#include <stdio.h>
int ptr[6] = {0,1,2,3,4,5};
#define N sizeof(ptr)/sizeof(int)
void foo(int ptr[n], size_t n) { // error: ‘n’ undeclared here (not in a function)
for (unsigned i=0; i<n; i++)
printf("%d ", ptr[i]);
}
void main(void) {
foo(ptr, N);
}
instead of compile-time:
#include <stddef.h>
#include <stdio.h>
int ptr[6] = {0,1,2,3,4,5};
#define N sizeof(ptr)/sizeof(int)
void foo(int ptr[N], size_t n) {
for (unsigned i=0; i<n; i++)
printf("%d ", ptr[i]);
}
void main(void) {
foo(ptr, N);
}
The Linux kernel [restrict .n] syntax is just too weird, almost perl-like, inventing new magic glyphs. And deviating from the normal restrict meaning.