Simple question, can we in c language cast void pointer by known size by unknown data type?
No. Every pointer type has a specific type to which it points. That includes void *, whose referenced type (void), is an incomplete type provided by the language. There is no such thing as a pointer to an unknown data type, though in void * you have a pointer type that is is interconvertible with all other object pointer types.
On the other hand, it is valid to access objects of any type via a pointer to a character type (e.g. char *, unsigned char *), and one way to do that is via an lvalue whose type is an array of characters. You could get something very similar to what you ask like so:
int assertEqual(void* expected, void* current, size_t size) {
unsigned char (*ep)[size] = expected;
unsigned char (*cp)[size] = current;
// ...
}
That makes use of a variable-length array type as the pointed-to type: pointers ep and cp each point to an array of size elements of type unsigned char. The size of that array type is size, so, for example, you will indeed find that sizeof(*ep) == size. That is, you have a pointer to an object of the specified size, through which you are permitted to access the bytes of that object.
As others have already pointed out, however, what you ask is kinda pointless. If you just want to compare two byte sequences without knowing what type of object they represent, then you can use memcmp(). On the other hand, that is not safe as a general equality test because two objects with different byte sequences can nevertheless be equal when compared as objects of a given type. This happens because many integer types are permitted to have padding bits that do not contribute to their values, and because floating-point representations are completely implementation-dependent.
Furthermore, struct and union types can contain padding bytes with unspecified, and not necessarily consistent values. You cannot compare compound types with the == operator, but comparing the byte arrays constituting the representations of such objects does not necessarily yield the same result as would comparing the objects on a per-member basis.
bsearchworks and it should all become clear. - Lundin