How do you convert '\0' to const void*?

Viewed 154

I'm migrating some code to u18, and it seems the poisix write function changed signatures

ssize_t write(int fd, const void *buf, size_t nbytes);

The call used to be write(output, '\0', 1);

However I would get the exception error: invalid conversion from 'char' to 'const void*' on u18.

What is the right way to initialize a null character array?

1 Answers

The correct way to initialize a C string character array would be "", which is an empty string literal containing nothing but a null terminator. The difference between "" and '\0' is that the former has a memory location - it is an array that decays into a pointer and so you can assign a pointer to it directly. While '\0' is just a character constant with no memory location.

However in case of Linux write, you don't need to provide a null terminated string. It works by checking the size parameter and does not look for null termination like a string handling function.

But if you want to write one single character - the null terminator - to a file, then the correct way would be:

write(output, "", 1);
Related