?
Solved

Output variable names in C++

Posted on 2003-03-04
11
Medium Priority
?
436 Views
Last Modified: 2012-06-27
Is there a way to output the names of declared variables in C++.
For example, if I have a program where I declare variables:

int i;
int j[30];
etc.
I want a way to output the names of specified variables,
so if name is the function that takes in some variable type I can do:
cout<<"Variables used are "<<name(i)<<" and "<<name(j);
so when running the executable I get:
Variables used are i and j.

Thank you,
Kayode
0
Comment
Question by:KayodeCS_BU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 8068082
#define name(x)  #x

For example, name( iInt ) will be "iInt"
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8068193
You can try typeid(), though I never used it though.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8068231
Use typeid(i).name() although this only works for built-in types and nonpolymorphic classes
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:n_fortynine
ID: 8068237
sorry i misunderstood your question, my bad
0
 

Author Comment

by:KayodeCS_BU
ID: 8068340
#include<iostream>
void NameOutput(int *a);
int main()
{
     int avariable =20;
     NameOutput(&avariable);
     return 0;
}
void NameOutput(int *a){
cout<<typeid(a).name();
}
Ok, this is what I have, when compiled using MVC++, I get the error : error C2059: syntax error : ';'
and the warning : : warning C4003: not enough actual parameters for macro 'name'
Can you tell me what I'm doing wrong?
p.s. thienpnguyen's define method worked but outputs a, instead of the variable name I am passing.
0
 

Author Comment

by:KayodeCS_BU
ID: 8068344
#include<iostream>
void NameOutput(int *a);
int main()
{
     int avariable =20;
     NameOutput(&avariable);
     return 0;
}
void NameOutput(int *a){
cout<<typeid(a).name();
}
Ok, this is what I have, when compiled using MVC++, I get the error : error C2059: syntax error : ';'
and the warning : : warning C4003: not enough actual parameters for macro 'name'
Can you tell me what I'm doing wrong?
p.s. thienpnguyen's define method worked but outputs a, instead of the variable name I am passing.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 8068450
#include <iostream>
using namespace std;

#define name(x) #x

int main()
{

    int i;
    int j[30];
    cout<<"Variables used are "<<name(i)<<" and "<<name(j) << endl;

    return 0;
}


Output will be

Variables used are i and j

====================================

Note:

Assume int a;
typeid(a).name() will give data type of a ==> int .
0
 

Author Comment

by:KayodeCS_BU
ID: 8068630
I understand what you are saying thienpnguyen, but I want to be able to use it in a function for example
#include<iostream>
using namespace std;
#define name(x) #x
void NameOutput(int *a);
int main()
{
    int avariable =20;
    NameOutput(&avariable);
    return 0;
}
void NameOutput(int *a){
cout<<"Variables used are "<<name(a);
}
Here the output is:
Variables used are a
I want the output to be:
Variables used are avariable
Thank you
0
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 400 total points
ID: 8069330
that name macro isn't doing what it looks like it's doing, when you say
cout<<name(i)<<endl;
it's not taking i as an integer variable then changing it to the variable name.
when you say #define name(x) #x
# when not used as the preprocessor character is a stringenizer <-i know i spelt that wrong
it works on the preprocessor level though
watch, try this:
char buffer[50] = {""};
strcpy(buffer,name(12345));
cout<<buffer<<endl;
it'll output
12345
cause it changed 12345 to "12345", so once the program was compiled name(12345) wasnt there, it was replaced with
"12345"
the same as name(i) was changed into "i"
what your trying to do doesnt exist, once the program is compiled the variables do not have names, they are just memory addresses.
int i;
i = 5;
i isn't "i", i is some memory address. the computer sees i as some memory address like 0x004ab76f (some hex number)
the reason for the name is so we dont have to deal with addresses.
sorry but what your tryingt o do is impossible. a lot of people ask how to get the variable names but i still don't understand what use the variable name would be..
why do you want it???
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8069910
>>understand what use the variable name would be

The only reason that I can think is that it would be useful in debugging if you didn't have a modern compiler.  You could output your var names and values using a generalized function instead of writing specific code for each spot you were curious about debugging.

The fact remains though, that this cannot be done.

I have to think that thienpnguyen was joking a little with his posts.    

Chase707
0
 

Author Comment

by:KayodeCS_BU
ID: 8075300
Thanks, I was going to use this as a debugging tool, but I guess I can use the memory addresses instead.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question