I have following code in a CRC16 generator
unsigned crc16dnp_byte(unsigned crc, void const *mem, size_t len)
what is default type for void const *mem
? is it char pointer
is mem
char pointer
?
thanks
I have following code in a CRC16 generator
unsigned crc16dnp_byte(unsigned crc, void const *mem, size_t len)
what is default type for void const *mem
? is it char pointer
is mem
char pointer
?
thanks
For discussion, we can put const
aside. It "just" modifies the objects pointed to by mem
to be read-only, independent of the underlying type. Please note that the pointer mem
is not constant, but this is just being picky.
Now we have a pointer with the data type void*
. Please note that there is no concept of a "default" type in C, all expressions have a defined type.
Assuming from the function's name, this pointer is used to calculate a 16 bit cyclic redundancy check value. Such calculations are done on the binary representation of the objects to check, independent of their data type. The theory behind CRC uses a bit as the type of the data. However, commonly the calculation is done with bytes, in C represented by char
.
The C standard says this to pointers (excerpts):
A pointer to
void
may be converted to or from a pointer to any object type.
When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object.
Therefore, to make life easy, the author chose void*
. You can provide a pointer to any object without the need to cast it or to think of alignment.