Looking for a circular buffer code for reuse, I came across using char, which confuses me
typedef struct CircularBuffer { void *buffer; // data buffer void *buffer_end; // end of data buffer size_t capacity; // maximum number of items in the buffer size_t count; // number of items in the buffer size_t sz; // size of each item in the buffer void *head; // pointer to head void *tail; // pointer to tail } CircularBuffer; void cb_push_back(CircularBuffer *cb, const void *item) { if(cb->count == cb->capacity) // handle error memcpy(cb->head, item, cb->sz); ////////////// here the part I don't understand ////////// cb->head = (char*)cb->head + cb->sz; ////////////////////////////////////////////////////////// if(cb->head == cb->buffer_end) cb->head = cb->buffer; cb->count++; }
Why cast this pointer to a void char? Is this some kind of C-idiom (I have very limited C experience)? A simple convenient way to increase the pointer, perhaps?
Using char for a position indicator reappears in another buffer:
typedef unsigned char INT8U; typedef INT8U KeyType; typedef struct { INT8U writePointer; INT8U readPointer; INT8U size; KeyType keys[0]; } CircularBuffer;
Again, this looks like some sort of handy trick that C programmers know about, something that pointers are easy to manipulate if they are characters. But I'm really just thinking.
source share