Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring array of voids

Tags:

c

memory

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?

like image 218
chavaone Avatar asked Apr 27 '12 08:04

chavaone


1 Answers

I don't understand why declaring an array of void is an error.

The technical reason you cannot declare an array-of-void is that void is 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 in

void foo;

Note that a pointer-to-void has a specific size, which is why

void *foo;

is valid, as well as sizeof(foo), but sizeof(*foo) would be invalid.

How can I declare a block of memory without a specific type?

For generic memory buffers, use an array of plain char or unsigned char (or even uint8_t). I usually prefer plain char when I need to pass pointers to the str* family of functions where array of unsigned char would lead to diagnostics about incompatible pointers.

like image 136
Jens Avatar answered Nov 15 '22 04:11

Jens