Thread: random variable names

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    52

    random variable names

    I know that we have structs, arrays and so on to avoid this, but only curiosity.

    In PHP (C based) i could create random variable names like $varXXX, where XXX is a random number. And i could do that forever (until the memory pops out xD)

    In C how can i do it?

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    you can't is the simple answer. Like you said, we have arrays and malloc to avoid this.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't. "Creating" variables at runtime is not possible in compiled languages. It is one of the advantages with (some) interpreted languages, such as PHP. In (nearly all) compiled languages, all variables have to be declared before they are used.

    There is usually a way to do the same thing, and it usually involves pointers and/or arrays.

    If you describe the actual problem you are trying to solve, I'm sure someone can come up with a suitable solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    52
    Its not a problem based question, just curiosity out of the box.

    Thanks!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not very easy in C, but in C++, it's usually a snap.
    What you would do is use a map to "translate" a string into a specified address or storage unit (variable of some type). In C++, there's an already existing map. In C, there is not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Interesting. We know that variable names can not be generated on the fly in compiled languages, but you can implement functions to handle a variable amount of arguments with the use of variadic macros.

    More food for thought.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    I was trying to come up with something sort of along the same lines. My problem was I wanted to be able to reference an array of structs based on the ID of the menu item being clicked to toggle them. The IDs don't start at 0 like an array though. Can an array/struct be defined in such a way that I can reference it using a variable? Like Templates.(VAR).Name? Or can arrays be defined to not start at 0? Like in VB6, you could do Array[400 to 500], etc. The easiest way I could figure to do it (short of requiring the control IDs to be sequential) was to put the menu ID in the structs and loop the array to find it (Struct[i].MenuId).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, what you propose is impossible in C. There are workarounds, but not exactly like you want.
    It is possible to do what you want in C++, however (although not exactly Templates.VAR.Name).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable names in Waterloo BASIC
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 12-27-2007, 04:57 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. underscore and variable names
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 09:57 AM
  4. random number between 0 and variable value
    By HybridM in forum C++ Programming
    Replies: 23
    Last Post: 02-06-2003, 10:14 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM