user4413591
user4413591

Reputation:

Is there a portable way to query the type of void* pointer?

Basic question: is there a portable (or at least, a library that is found in most compilers) way to query the type of a void* pointer at runtime? In particular, is it possible to determine if the pointer is of type struct x or of type struct y (as an example).

Additional info: I know gcc offers the typeof operator, but I want a more portable, less compiler-dependent way of accomplishing the same thing.

Requirements:

  1. Must not be completely compiler-dependent. This includes compiler-specific macros and other features not commonly implemented in most compilers.

  2. I would prefer a function that works on any compiler over any less portable implementation (though I will accept the answer that best suits my goals).

  3. It is okay to suggest using a function in a library that is not in the C11 function, but is commonly found in most compilers. Make sure you explain how it works and the arguments though.

Upvotes: 0

Views: 172

Answers (1)

user149341
user149341

Reputation:

What you're asking for is impossible. A void * pointer is by definition a generic pointer. It can be cast to or from a pointer of any type; there is no way to determine what type of data (if any!) it points to.

Upvotes: 6

Related Questions