How do you check if a pointer, in C, is of a certain type?

Gcc does not put the structure definition anywhere in the runtime. This means you cannot as standard.

It can depend on what you are using the type information for. Two major applications might be:

  1. Debugging or similar runtime inspection
  2. Serialization and Deserialisation of datastructures

In the first case there information is often available stored in the symbols output by the compiler and attached to the executable (in many environments).

The implementation is platform specific and often means the compiler needs to be instructed to output this information. One example of a program doing this is gdb. Pointers still have to be typed correctly for this to be useful.

For serialisation types are often tagged with values like you suggest. These tags don't though have to be stored with the in-memory data. They can be added by the output routine.


"I am trying to avoid putting idnumbers into my structs to identify their type." Don't avoid that. If you really want to be able to check type, put a typeID as the first element of every struct. Your impulse was not a bad one.


You can't.

A pointer merely stores an address and nothing related to the contents of that address.

Tags:

C

Gcc