Thread: Using a variable's value to reference a variable name?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    84

    Using a variable's value to reference a variable name?

    Hi,

    I'm trying to see if it is possible to store a variable name in a variable, for later use. I guess the closest thing I've seen to this might be PERLs hash variables.

    Here's the issue...

    I have a big file that, for all intents and purposes, is structured similar to an mp4 container. Just a bunch of boxes inside boxes. Like mp4 container, not all box are mandatory.

    I would like to store the root box names in a struct array. Then when processing the individual root boxes, create another struct array using the name of the root box as the variable name.

    Is this possible at all? My searches really don't come up with anything useful (mostly because the search terms I use are too basic, and I don't know what proper term(s) to search for).

    Ty.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,411
    Basically, you want to map root box names to struct objects. As per the hint from Perl's hash variables, one way is to hash these names as strings so that they become indices in some kind of hash table that serves as a map. Another way is to construct a balanced binary tree using the names, though these have different tradeoffs, e.g., in time complexity.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,410
    In an interpretted language you can do that, but in a compiled language like C, the variable names disappear from the executable code (although they can appear in debugging infomation).

    You should come up with another strategy. You mentioned an array of structs. How about an array of arrays of structs? Then you could have the section names with the index of that section's position in the array of arrays.

    If the names are in sorted order then they can be searched efficiently with binary search, although if there's only a handful of them a linear search is probably good enough.
    Code:
    Index
    alpha   0
    beta    1
    gamma   2
    
    Data
       0      1      2      3      4
    0  struct struct struct struct 
    1  struct struct struct
    2  struct struct struct struct struct
    What is the maximum number of sections?
    What is the maximum number of structs per section?
    A long time ago being crazy meant something. - Charles Manson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,411
    Having said that, I realised that you wrote "not all the boxes are mandatory". Does this mean that all the root box names are known in advance? If so, then maybe you're overthinking it: a struct of struct pointers, where the member names of the outer struct correspond to root box names, might suffice. A null pointer would indicate that that box was omitted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    84
    Tyvm you two, I guess it makes sense. I'll just need to move past my flaky understanding of pointers.

    I guess this is as good a time to start. I may be back with more questions tho.

    Ty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  2. Variable Reference Question
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2007, 09:36 AM
  3. destroyed reference variable
    By Mehdi in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2006, 12:54 AM
  4. problem with variable passed as reference
    By crash88 in forum C Programming
    Replies: 1
    Last Post: 05-16-2006, 07:16 PM
  5. Passing the variable as a Reference
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 02:35 PM

Tags for this Thread