-1

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

Dai
  • 141,631
  • 28
  • 261
  • 374
jopa1351
  • 21
  • 3
  • 3
    What do you mean by "default type"? A `void const*` **is** a type... – Dai Oct 04 '22 at 06:21
  • 5
    The variable `mem` is a pointer to constant data. That's it. `void *` can point to anything, there's no "default" type. – Some programmer dude Oct 04 '22 at 06:22
  • Maybe [this](https://stackoverflow.com/questions/1171839/what-is-the-unsigned-datatype) has answers to your question. – mmixLinus Oct 04 '22 at 06:28
  • thanks so basically void const *mem is a type and points to constant data that can be an array of ints – jopa1351 Oct 04 '22 at 07:30
  • 1
    `mem` could be pointing to an array of `int` values, a single `struct` object, an array of `struct` objects, or even a single `double` value. It doesn't matter what it's pointing to. Judging by the name, the function will probably just treat it as a pointer to an array of bytes. – Some programmer dude Oct 04 '22 at 07:58

1 Answers1

0

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.

the busybee
  • 10,755
  • 3
  • 13
  • 30