Monday, May 24, 2010

In C how do you extract the integer component of a char* array?

I am extracting argv from the input: main(int argc, char* argv[])


one of my inputs is an integer that I need to be an integer for a function. How do I use argv[2] as an int instead of a char*. Currently the compiler cannot convert. =(

In C how do you extract the integer component of a char* array?
Argv[] contains the command-line arguments, all as strings. It's your job to convert the appropriate string to an integer, I'd suggest using atoi(). Be sure to handle error conditions, though, as when argv happens to be alphabetic.





Hope that helps.
Reply:My C is a bit rusty here.. but why couldn't you create a pointer for an integer and point it to the address of argv[2]. The compiler shouldn't complain about that.....





hope that helps.


No comments:

Post a Comment