Hacker News new | ask | show | jobs
by tpoacher 1160 days ago
> But then I can't use sizeof() if I pass that buffer into a function via a pointer.

Obviously, this isn't necessarily an improvement, but in theory you could do this:

  size_t getlength( int m, char (*p) [m] ) { return sizeof *p; }
where you'd be calling it like this:

  char mystring[] = "Hello";
  getlength( sizeof mystring, &mystring )
But then you're back in "having to pass the length as a separate parameter" territory, I guess. (but at least it's the length of the array here, not just the zero-terminated component, which is what you wanted).