6

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?

Jens
  • 69,818
  • 15
  • 125
  • 179
chavaone
  • 183
  • 3
  • 9

5 Answers5

13

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.

Jens
  • 69,818
  • 15
  • 125
  • 179
5

Use an array of char or unsigned char instead.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • +1, I believe the OP is unaware that this is the ubiquitous `byte`-sized type in C given `void*` is the datatype requested by `read()` and `write()`. – user7116 Apr 27 '12 at 08:21
2

When declaring an array, the compiler must know the size of the array. The void type has no size. So compiler report error.

RolandXu
  • 3,566
  • 2
  • 17
  • 23
  • 2
    More correct would be: the compiler has to know the size of the *element type*. The array size itself may be variable length. – Jens Apr 27 '12 at 08:40
1

This is how you do it, although it is not an array of voids as you may have wanted, it is an array of void pointers; in each cell of this void pointer you can assign ANYTHING.

If you wish to store a variable, you simply cast it to (void*), here is an example:

void *XPoint[5] = { 0 };
XPoint[0] = (void*)5;   // int
XPoint[1] = "Olololo!"; // char*
XPoint[2] = (void*)'x'; // char
XPoint[3] = (void*)structA.nNum; // struct int 
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
-1

You also can declare void pointer:

void* buffer;

// just pointers for simple work with allocated piece of memory
char* chBuf;
long* intBuf;

then use malloc() for this pointer and work with this piece of memory like with an any array (char, short, long, float, double or your own type). Don't forget to use free() to avoid memory leak in this case.

buffer = malloc (BUFFER_SIZE);
if (buffer==NULL)
  exit (1);

// init our buffers
chBuf=(*char)buffer;
intBuf=(*long)buffer;
vard
  • 2,142
  • 4
  • 30
  • 40
  • 3
    Don't cast the output value of `malloc` in C. – Daniel Kamil Kozar Apr 27 '12 at 08:26
  • That's in the C FAQ. Read it. Live it! – Jens Apr 27 '12 at 08:41
  • When you do `buffer = malloc(BUFFER_SIZE);` the return value of `malloc` is casted automatically to the type of buffer. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – chavaone Apr 27 '12 at 08:43
  • 1
    @chavaone No, it's not *casted* (which requires a cast operator). A `void` pointer in C is simply assignment-compatible to all pointer-to-object types. – Jens Apr 27 '12 at 09:03