Thread: Can I use a value of the variable as a name?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    Yeah 'no' is the answer. Since your 'a' variable is an int, why not just build an array of pointers to struct pro and index it that way?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A better question might be: why do you think you need to do that?
    You could probably simulate it with a map-like structure.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It sounds like you want an an associative array. Many high level languages have this feature built in. In python you would use a dictionary.

    I implemented something like this once in C, but its quite tedious. The way I did it was to have a linked list of structs holding a string for the variable name, an integer enumerating the datatype, and a void pointer holding the data. To look up a variable i'd then run through the list using strcmp to find if it existed. Perhaps its not the most efficient way of doing it, but it worked. That said theres probably libraries around that can do this stuff better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM