I’m writing a program that copies files. I’ve used a buffer in order to store the information that the read() function provides and then give this data to the write() function. I’ve used this declaration:
static void buffer[BUFFER_SIZE];
The problem is that I get the the error error: declaration of ‘buffer’ as array of voids.
I don’t understand why declaring an array of void is an error. How can I declare a block of memory without a specific type?
The technical reason you cannot declare an array-of-
voidis thatvoidis an incomplete type that cannot be completed. The size of array elements must be known but the void type has no size. Note that you are similarly unable to declare a void object, as inNote that a pointer-to-void has a specific size, which is why
is valid, as well as
sizeof(foo), butsizeof(*foo)would be invalid.For generic memory buffers, use an array of plain
charorunsigned char(or evenuint8_t). I usually prefer plaincharwhen I need to pass pointers to thestr*family of functions where array ofunsigned charwould lead to diagnostics about incompatible pointers.