Data types at run time 
Author Message
 Data types at run time

Does anyone know how to determine the data type of a variable at runtime?

I am trying to write some code that will convert the value of a variable
to string without knowing a priori its data type. The function will be passed
a pointer to the variable. How can this function determine the variable's
data type. The only thing I DO know is that this variable will never be
a structure or a union; it CAN be any numeric (including float and double) or
character (including string) type;

Please send e-mail and I will summarize to the net.

#include <standard/disclaimer.h>  |  LIFE IS NOT A MALFUNCTION! - Number 5
    ____  ___ _  _  __ ____     ____    _ _  ___  ___   _ _   ____
    /  / /  /  | /  /  /  /      /     //// /  / /  /   / /  /___
   /  / /--/   |/  /  /  /   /  /     /  / /--/ /--\   / \      /
  ~~~~ ~  ~    ~  ~~ ~~~~    ~~~  ~  ~  ~ ~  ~ ~   ~  ~  ~  ~~~~
PONY EXPRESS:   David J. Marks  M/S 3520, Texas Instruments, Erwin Highway
                                P. O. Drawer 1255, Johnson City, TN. 37605

ELECTRIC AVENUE: ...!mcnc!rti!tijc02!djm408          MA BELL: 615-461-2074



Wed, 19 May 1993 19:40:00 GMT  
 Data types at run time

Quote:
>Does anyone know how to determine the data type of a variable at runtime?

There's no built-in way in C to do this, so you'll have to implement
it yourself.

One way to implement it would be to pass a second argument to the
function that accepts the pointer.  This argument should be an
indicator of the type (it should probably be an enum).

A variant would be to pass a struct instead of the pointer, something
like:

struct {
  enum {type_int, type_char, ...} type;
  union {int, char, ...} *ptr;
  } argument;

Your routine can then contain
  switch (argument.type) {
    case type_int: ...
    case type_char: ...
    ...
  }

Barry Margolin
Thinking Machines Corp.


{uunet,harvard}!think!barmar



Wed, 19 May 1993 22:56:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Determine Column Data Type at run time

2. Determining type at run-time or compile-time

3. run time type checking

4. Choose type of the object in run-time

5. Bug in C# run-time type information?

6. Q: malloc and type info - fatal run-time eror

7. fast run-time type idenitification

8. Run-time type define

9. Run-time type information

10. Enabling Run-Time Type Information: how?

11. Does run-time type info work correctly?

12. Run-Time Type Information (RTTI) among dlls

 

 
Powered by phpBB® Forum Software