Path: utzoo!utgpu!watmath!clyde!ima!think!barmar
From: barmar@think.COM (Barry Margolin)
Newsgroups: comp.lang.c
Subject: Re: Data types at run time
Message-ID: <33234@think.UUCP>
Date: 9 Dec 88 22:56:41 GMT
References: <293@tijc02.UUCP>
Sender: news@think.UUCP
Reply-To: barmar@kulla.think.com.UUCP (Barry Margolin)
Organization: Thinking Machines Corporation, Cambridge MA, USA
Lines: 30

In article <293@tijc02.UUCP> djm408@tijc02.UUCP (David Marks         ) writes:
>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.

barmar@think.com
{uunet,harvard}!think!barmar