When are values implicitly converted to pointers?
In practice, integer types may be converted to pointers implicitly by the compiler. In theory, this is illegal and compilers that accept it will usually issue a warning:
example.c:2:6: warning: incompatible integer to pointer conversion initializing
'void *' with an expression of type 'int'
In your above example:
char in = 5;
write(in, 20);
char
is an integer type, so if your compiler allows it, it may be implicitly converted to a pointer type, although it is not part of the C standard and is completely compiler-specific.
Note that converting an integer type to a pointer type using a cast is allowed by the standard, although results are implementation-defined:
char in = 5;
write((char *)in, 20);
The only allowed case of implicit conversion is when integer constant 0
, which denotes a null pointer:
write(0, 20); // allowed
Note that the integer constant is itself allowed, but a variable of integer type with the value 0
is not:
char in = 0;
write(in, 20); // not allowed
As for the others, when you pass a pointer, you don't need the &
, obviously, because it's already a pointer. When you pass an array, it decays to a pointer so you don't need &
there either. In both these cases it would be actually illegal to use it, as your function above expects a char *
and be fed with a char **
and a char (*)[20]
respectively.