Thread: making a variable with others

  1. #1

    making a variable with others

    I've got alot of experience with PHP, and have recently started working with c/c++... I was wondering if it is possible to make a variable whos name is the value of another string variable, in php this would look like:
    $var = "var2";
    ${$var} = "hi";
    that would make the variable $var2 have the value of hi... If this is possible in c/c++ can someone please tell me how to do it?

  2. #2
    i too come from a good php background, and am yet to see this implemented in a simple way

    I would think that using pointers would be the best way to go about this, although you wouldnt be able to actually name them...

    If anyone has a simple way to do this, i would be grateful

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    In short, no. Because C++ is a compiled langauge, unlike PHP, after the program has been linked, variable names are essentially gone. You can't look at an executable and see variable names. You can somewhat emulate the feature with maps and pointers, but it's not nearly as easy or flexible as with an interpretted langauge. Welcome to the limitations of programming langauges that aren't slow .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    What about the stringizing operator? For example,

    int foo=0;
    string temp=#foo;
    cout<<temp;//should output foo

    I've never had the need to use it though so I know very little about the details.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Just use a constant reference!
    #include <iostream>
    int main()
    	char* foo = "Hi";
    	char* const& bar = foo; // bar is constant reference to foo
    	std::cout << bar; // outputs foo
    	return 0;

  6. #6
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    This works in Borland C++ 5.02 and should work on most compilers.
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #define CV(x) char* aa##x##bb
    #define V(x) aa##x##bb
    int main()
       int n;
       cin >> n;
       for (int i = 0; i < n; i++)
          CV(i) = new char[100];
          sprintf(V(i), "The variable name is : aa%dbb", i);
          char temp[100];
          strcpy(temp, V(i));
          cout << temp << endl << V(i) << endl;
          delete[] V(i);
       return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM