Get Variable Contents

This is a discussion on Get Variable Contents within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there was any way, if possible, to retrieve the variable contents? For example: Code: const char* ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Get Variable Contents

    I was wondering if there was any way, if possible, to retrieve the variable contents? For example:

    Code:
    const char* variable1 = "variable2";
    const char* variable2 = "test";
    
    std::cout << variable1;
    That code outputs "variable2", but I want it to output the contents of "variable2" (I'm using this in a loop where I have an array with different variable names, so thats why I cant just replace variable1 with variable2 in the above example.)

    Any help is greatly appreciated. Thanks.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, no, C++ does not have a variable variable feature, as does exist in like PHP.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for the information...I just figured out how to accomplish what I was trying to do without a "variable variable". For those who are asking the same question, this is what I did:

    Code:
    const char* var1 = "test";
    const char* var2 = "";
    const char* var3[] = {var1, var2};
    int a = 2;
    int c = 0;
    
    for(int b=0; b<a; b++) {
         if(var3[b]!="") {
              var3[c] = var3[b];
              c++;
         }
    }
    
    while(c<a) {
         var3[c] = "";
         c++;
    }
    That's basically all I needed (to remove variables that were blank).

    EDIT: This is not a very good example because var3 at the end will equal var3 at the beginning, but this code works for what I was trying to accomplish.

    EDIT EDIT: What the above code does is moves the blank entries to the end of the array.
    Last edited by stickman; 04-16-2006 at 11:47 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    An easier way is to use pointers, which you almost did. Pointers is a kind of variable variable if you would write:

    Code:
    const char* variable2 = "test";
    const char* variable1 = variable2;
    
    std::cout << variable1;
    or:
    Code:
    const char* variable2;
    const char* variable1 = variable2;
    strcpy(variable2, "test");
    
    std::cout << variable1;
    it would output the contents of variable2. Notice that you got to declare variable2 before pointing to it, or you won't be able to compile.

    In this way it's also possible to write:
    Code:
    int variable1 = 5;
    int* variable2 = &variable1; //The & before a variable gets its memory address
    std::cout << *variable2; //The * before a pointer gets what's stored at the memory address,  will result in 5
    variable1 = 10;
    std::cout << *variable2; //Returns 10
    Maybe you knew this, but from what I understood - this is what you asked about.

    Daniel

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for the information. I've never really understood that & and the * and pointers and memory address things (and I know I should attempt to understand them because its a big part of c++).

    I think I've accomplished what I wanted to do, but thanks for the information.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by The Wazaa
    An easier way is to use pointers, which you almost did. Pointers is a kind of variable variable if you would write:

    ...

    Code:
    const char* variable2;
    const char* variable1 = variable2;
    strcpy(variable2, "test");
    
    std::cout << variable1;
    it would output the contents of variable2. Notice that you got to declare variable2 before pointing to it, or you won't be able to compile.

    ...
    No, variable2 is an uninitialized pointer. You can't just copy data to it (safely) without first allocating space for it to point to.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Oh, I thought that strcpy() did that for you. At least I know that it can extend the allocated space, but when you say it I'm not sure that it allocates new space for you. *Testing* Oh, didn't work. Sorry about that.

    EDIT: fixed it :
    Code:
    char* variable2 = new char;
    char* variable1 = variable2;
    strcpy(variable2, "test");
    
    std::cout << variable1;
    Last edited by The Wazaa; 04-17-2006 at 05:40 AM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by The Wazaa
    Oh, I thought that strcpy() did that for you. At least I know that it can extend the allocated space, but when you say it I'm not sure that it allocates new space for you. *Testing* Oh, didn't work. Sorry about that.

    EDIT: fixed it :
    Code:
    char* variable2 = new char;
    char* variable1 = variable2;
    strcpy(variable2, "test");
    
    std::cout << variable1;
    You expect to copy 5 bytes into a pointer that only points to a buffer of a single byte?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > const char* variable1 = "variable2";
    > const char* variable2 = "test";
    > std::cout << variable1;

    Maybe something like
    Code:
    std::map<std::string, std::string> table;
    table["myvariable"] = "test";
    Then later on, you can do
    Code:
    cout << table["myvariable"];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Quote Originally Posted by hk_mp5kpdw
    You expect to copy 5 bytes into a pointer that only points to a buffer of a single byte?
    It works for me, I think I've read somewhere that if the memory allocated in the char* is too small then strcpy makes it bigger. Or maybe I've misinterpreted something about that.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    no strcpy will never ever touch the memory.

    It works because you were lucky. This program crashes for me for example:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main() 
    {
    	char foo[] = "abc";	// size = 4 bytes.
    	cout << "foo: " << foo << endl;
    	strcpy(foo, "abcdefgh");
    	cout << "foo: " << foo << endl;
    }
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Hm, strange. I've used things like that forever and never experienced a crash because of that :-/ Well, got to fix that now, and I will not post anything more in this thread since this went a bit off topic. Thanks for the information .

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    If I only knew just a little bit about pointers and memory allocation stuff...

    Just wondering, is it bad to have like 6 char[255] variables? Thats like a total of (6 * 255) = 1530 units (whatever these are measured in, I'm assuming bytes).

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    That's not a lot of space at all, although you would often be better served to have 6 string variables instead.

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Well, the way I use these variables, I cant use anything except char[x] where x represents a number.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  4. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21