Same ‘error’ did not show up in a bare cpp test app (VSC but not using Workbench),
And sizeof(arrd) did print out “128”,
So I tested/searched further…
And few nuances, for strncpy() I could pass in a sizeof() for the 3rd arg but it’s compiled into a constant anyway right? So doing strncpy() on any yet-to-be-generated (variable) strings may not work properly. I guess one best practice is #define a standard buffer length for all C-strings and just stick to that as the absolute max string length for all manipulations. And could make a custom function wrapper for strncpy() to -
Always clear the destination array (use a for loop w/ arrd[i] = ' ')
Do strncpy()
Put ‘\0’ at end of copied string up to size of buf
Also I should change declaration of ‘wrapper function’ to this (pointers to arrays, not copies of the arrays), with void return since it’s actually manipulating the global instance of arr1:
For dynamically created variables sizeof() is not determined at compile time but at runtime. So it will work.
A better way to clear an array would be
memset(arr, 0, sizeof(arr));
This will also take care of 3.
I'd rather have your function either return the length of the resulting string (like snprintf() or
do the same as strncpy() and return the pointer to the string for direct handling of the result.