Results: What would be the most useful search utility? 

Voters
0 You may not vote on this poll

  • Enter a search term - results are based on your recent history of pages (e.g. i know i've seen a page about this recently!)
    0%
  • Picture searching with some recognition of colour to identify people or scenery etc. matched with your query)
    0%
  • Highly localised searching (e.g. type in "pizza delivery" and only get results within 5miles of your location)
    0%
  • Google2
    0%
Multiple Choice Poll.
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0

    Is is possible to set a #define to hold user input?


    Hi there,

    As you may well have seen from my previous posts i am extremely new to C - less than a week! Can anyone tell me if it is possible to set the value of a
    Code:
    #define
    to be a user input? An example would be (and the reason i am asking) is, i need to add a number of spaces to a string. Can i require a command line arguement that sets this to (say) argv[3]?
    e.g.
    Code:
    #define argv[3]
    I doubt it is possible, but as i am very new to C can someone suggest an alternative way of making this value passed at runtime available? I cannot really see a way of making it a "global variable" as it is main that would originally be parsed the value at argv[3].

    I hope you can see what i am getting at - the heat doesnt make for clear thinking :)

    Cheers in advance,

    aceduk
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    #define values are only available to be set during design time.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0
    Thanks, pretty much as i expected.

    Can anyone suggest the best way of making this value accessable then?
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    just use a global variable and set that to argv[3]
    i.e.
    define the variable outside of the main() function
    Code:
    int iNumSpaces = 0;
    int main(int argc, char *argv[]) {
    ...
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0
    D'oh! Yeah, now that someone says it - it's obvious.... sorry about that!

    Thanks for the swift response.
  10. #6
  11. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    8
    Rep Power
    0
    I might be misunderstanding, so figure me if this is off-base. But I'm guessing you want a value, passed in at argv[3], to be available not only to main, but to other files in your project as well.

    It seems that you could define an external variable in the main file (thus being accessible to the other files), and upon entering main, set the variable equal to argv[3]. Like

    PHP Code:
    extern int myGloabal;

    int main(int argccharargv[]) {
         
    myGlobal=atoi(argv[3]);

    Maybe this will work for you? If it is the first line in your main, the value should be available to anything else. Note that you cannot use this value to initialize non-dynamic arrays, if that is what you are trying to do. Those must have constant values at compile time. You can use it with dynamic arrays tho. But anywho, give this a shot.

IMN logo majestic logo threadwatch logo seochat tools logo