Suppose I have a function
void foo(char *)
which, internally, needs to treat its input as a block of NUL-terminated bytes (say, it's a hash function on strings). I could cast the argument to unsigned char*
in the function. I could also change the declaration to
void foo(unsigned char *)
Now, given that char
, signed char
and unsigned char
are three different types, would this constitute an interface change, under any reasonable definition of the term "interface" in C?
(This question is intended to settle a discussion raised by another question. I have my opinions, but will not accept an answer until one comes up as a "winner" by the votes of others.)
I choose "C -- none of the above."
Although it's not a direct answer to the question you actually asked, the right solution to the situation seems fairly simple and obvious to me: you shouldn't really be using any of the above.
At least IMO, you have a really good reason to do otherwise, your function should accept a
void *
or (preferably)void const *
. What you're looking for is basically an opaque pointer, and that's exactly whatvoid *
provides. The user doesn't need to know anything about the internals of your implementation, and since any other pointer type will convert tovoid *
implicitly, it's one of the few possibilities that doesn't break any existing code either.Does a char* implicitly convert to an unsigned char*?
details.
According to ISO/IEC 9899:TC3,
char
,signed char
andunsigned char
are different basic types (6.2.5 §14) and thus incompatible (6.2.7 §1), which is also explicitly mentioned in footnote 35 on page 35So yes, this is clearly a change to the programming interface.
However, as
char *
,signed char *
andunsigned char *
will have identical representations and alignment requirements in any sane implementation of the C language, the binary interface will remain unchanged.No it isn't. Any change to client code is trivial (especially if it's just to avoid a warning), and in practice in pretty much any C implementation you'll find that you don't even need a re-compile because pointers to
char*
andunsigned char*
will be passed exactly the same way in the calling convention.Yes it is. Client code which previously compiled will no longer compile (or anyway is likely to generate new warnings), so this is a breaking change.