• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Output variable names in C++

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
KayodeCS_BU
Asked:
KayodeCS_BU
  • 4
  • 3
  • 2
  • +2
1 Solution
 
thienpnguyenCommented:
#define name(x)  #x

For example, name( iInt ) will be "iInt"
0
 
n_fortynineCommented:
You can try typeid(), though I never used it though.
0
 
n_fortynineCommented:
Use typeid(i).name() although this only works for built-in types and nonpolymorphic classes
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
n_fortynineCommented:
sorry i misunderstood your question, my bad
0
 
KayodeCS_BUAuthor Commented:
#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
 
KayodeCS_BUAuthor Commented:
#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
 
thienpnguyenCommented:
#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
 
KayodeCS_BUAuthor Commented:
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
 
PlanetCppCommented:
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
 
Chase707Commented:
>>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
 
KayodeCS_BUAuthor Commented:
Thanks, I was going to use this as a debugging tool, but I guess I can use the memory addresses instead.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now