Search code examples
cposixunsignedsigned

Should POSIX `read()`'s `buf` be `signed` or `unsigned`? Does it even matter?


POSIX read function is defined as ssize_t read(int fd, void *buf, size_t count);, taking its buf argument as void*.

Does it matter if the actual buffer passed in is an array of chars or unsigned chars? If so, which one should I use? I googled and read the man, but even the type of the buffer isn't mentioned, let alone its signedness.


Solution

  • The reason for having the declared type void * is that you can read pretty much any type. You can read a char. You can read an unsigned char. You can read an int, if what you wrote to the file earlier was also an int. You can read a struct div_t, if that is what was written to the file.

    Pick whatever type was written to the file, or if you're reading arbitrary bytes, whichever type works best for your later processing.