I have a lot of functions that expect a string as argument, for which I use char*, but all my functions that expect a byte-array, also use char*.
The problem is that I can easily make the mistake of passing a byte-array in a string-function, causing all kinds of overflows, because the null-terminator cannot be found.
How is this usually delt with? I can imagine changing all my byte-array functions to take an uint8_t, and then the compiler will warn about signed-ness when I pass a string. Or what is the right approach here?
The problem is more general in C than you are thinking. Since
char*andchar[]are equivalent for function parameters, such a parameter may refer to three different semantic concepts:charobject (this is the “official” definition of pointer types)chararrayIn most cases where is is possible the mondern interfaces in the C standard uses
void*for an untyped byte array, and you should probably adhere to that convention, and usechar*only for strings.char[]by themselves probably are rarely used as such; I can’t imagine a lot of use cases for these. If you think of them as numbers you should use thesignedorunsignedvariant, if you see them just as bit patternunsigned charshould be your choice.If you really mean an array as function parameter (
charor not) you can mark that fact for the casual reader of your code by clearly indicating it:This is equivalent to
but makes your intention clearer. And in the future there might even be tools that do the bounds checking for you.