Thread: Variable Call via Another Variable's Content

  1. #1
    Registered User
    Join Date
    Jan 2005

    Variable Call via Another Variable's Content


    Does anyone know of a way to call (or create) a variable by using the *contents* of another variable, presumably a concatenated string?
    An attempt I didn't expect to work is below to clarify...if it clarifies.

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
        const char a='B';
        const int B=99;
        const char* A=a;
        int ending;
        cout << a << ": Should read 'B'" << endl;
        cout << B << ": Should read '99'" << endl;
        cout << A << ": Should read 'B'" << endl;
        cout << *A << ": Should read 'B'" << endl;
        cout << &A << ": Should read some memory address crap" << endl;
        //cout << *a << endl;
        cout << "Press enter to end program" endl;
        cin >> ending
        return EXIT_SUCCESS;
    Does what I'm asking make sense? Is there a way to do it?
    Thanks muchly!

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    const char* A=&a;
    Will have A be an unchangeable pointer to the character variable a.

    However, this is just a pointer. I think you want to do something like:
    const char a = 'B';
    char thestring[50];
    thestring[0] = a;
    thestring[1] = '\0';//make sure you add the null
    strcat(thestring, "ecause I said so.");//this will concatenate to the string.
    //now thestring is an array that contains "Because I said so."
    However, that's the C-style way. The standard C++ approach is something like:
    std::string thestring(1u, a);//create a string with 1 of the variable a's in it
    thestring += "ecause I said so.";//add this to thestring
    //now thestring has a value of "Because I said so."
    //be careful, though.  If you want to access a std::string like a C-style string, you should use
    //the member function c_str().
    //an example:
    char string2[50];
    strcpy(string2, thestring.c_str());
    Note that the above code isn't the best -- it can definitely be made better by using dynamic allocation, but I decided to stick with simplicity here.
    Last edited by Kybo_Ren; 01-12-2005 at 11:12 PM. Reason: fixed table breaks

  3. #3
    Registered User
    Join Date
    Jan 2005

    Alright; what about creating dynamic or variable-variables?

    Thanks Kybo_Ren,

    That will definitely help me in getting the strings right, but I'm still in the dark about creating a variable using the contents of one variable as the name of the new one.

    char x[50];
    x[1]='/0'; //could I just do x[0]='F/0'?
    strcat(x, 'rank');
    //good to here. I have x=Frank thus far
    Now, conceptually, I want to declare a variable named "Frank". But I don't just want:

    int Frank=2;
    I want something more like:

    int somesupermagictricktousethecontentsof(x)=2;
    cout<<Frank; //displaying the value 2
    This way I can do fancier things. Does C++ have a way to accomplish this? Or perhaps..perhaps I need to write a function that returns the content of x and use it like I did just above....

    Any further advice?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Call a funvtion from a variable
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-18-2009, 07:22 AM
  2. Variable Properties vs Public Variables
    By Rainbowinblack in forum C# Programming
    Replies: 2
    Last Post: 02-19-2008, 01:48 PM
  3. Working with variable variables
    By Differentialpi in forum C Programming
    Replies: 1
    Last Post: 01-06-2008, 10:35 PM
  4. accessing the variables in variable length parameters
    By Dave Jay in forum C Programming
    Replies: 4
    Last Post: 03-13-2005, 01:36 AM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM