Type name from Variable

R

rohitjogya

Hi friends,
I want to print data type name of some variables...
there is a function called typeof(var) which returns data type of var
but want to print it so i need the result in char* format
is there any method for that??

Please help me out
 
J

jaysome

Hi friends,
I want to print data type name of some variables...
there is a function called typeof(var) which returns data type of var
but want to print it so i need the result in char* format
is there any method for that??

There is no way to do this in Standard C. gcc implements this as an
extension, but even at that, I don't think it does what you want.

If you can print what you desire from "typeof", then surely you must
have access to and are able to modify the source code. If so, why on
Earth would you want to print out the type of a variable? Certainly
the source code tells you unambiguously what the type is. In C, unlike
in some other languages, the true type of an object is always known at
compile time.

If you could provide more details on what you are trying to
accomplish, perhaps we could give you some good advice.


Best regards
 
R

rohitjogya

Actually i m trying to write a preprocessor,
In that preprocessor i m replacing any assignment with a macro which
logs the assignment in a logfile..

in that for logging purpose, i need to know the type of rvalue n
lvalue..
any solution for this??
 
S

SM Ryan

# Hi friends,
# I want to print data type name of some variables...
# there is a function called typeof(var) which returns data type of var
# but want to print it so i need the result in char* format
# is there any method for that??

There is no ANSI C solution, and precious few nonstandard solutions.
Objective-C does have this extension as one of their @-thingies.
 
S

SM Ryan

# Ok can anyone tell me if GNU gcc offers any symbol table functions ??

It doesn't. It can write symbol table to the object file for debuggers
if you want to decode that.
 
R

rohitjogya

typeof(var) returns data type of the var
so can we have something like this..


#define typestring(X) #X

...
int a;
char *type = typestring(typeof(a));
....

but this is not working...
macro returns string typeof(a) ..
i want to pass result of typeof(a) to the macro
how can we do that?
 
P

Peter Nilsson

typeof(var) returns data type of the var

It might on your systems, but it is not a standard
C function. As such, we cannot assist you with it
in comp.lang.c.

BTW, please quote relevant context when replying.
 
S

SM Ryan

# typeof(var) returns data type of the var
# so can we have something like this..
#
#
# #define typestring(X) #X

#defines and all that occur prior to parsing.

# char *type = typestring(typeof(a));

The typeof can only makes sense as the context
sensitive parsing occurs.

# but this is not working...
# macro returns string typeof(a) ..

Because typeof(a) could not be evaluated during preprocessing.

This is why _function_ is character string variable, not a #define.
It requires information from the parser not available to the
preprocessor.

# i want to pass result of typeof(a) to the macro
# how can we do that?

You're screwed. Sorry. You need a parser that is willing to
share this information; except gnu objective-c, I don't know
any other parser that does this. I don't know of any parser
that even generates old-fashionned cross-reference output
that you could use for this.

The parser obviously has this information available, and if made
available would solve var-args induced bugs, but it simply isn't
done. (Historical inertia?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top