0

I am getting input from terminal using "char *argv[]" parameter in main function. Now, I want to check whether all the inputs are not character (they are not necessarily int, they can be double) but I don't know how to do it here because inputs are pointers and it's not straightforward like when you get inputs from scanf() so just checking the ascii characters or something like that. Could someone help me with the issue?

2
Contributors
3
Replies
17
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
Featured Replies
  • argv[1] is a pointer to a string, if you want the first character in the string then it's argv[1][0] this makes more sense anyway `argv[1][0] < '0' || argv[1][0] > '9'` Read More

0

Of course it's identical to the way you would validate input from scanf(). argv is just an array of strings. argv[0] is always (if the os supports this) the name of the executable program and each string after that is whatever you put on the command line. If you execute your program named "test.exe" like this:
c:\test one two three <Enter key>

Then

  • argv[0] = "test.exe" (sometimes it also has the full path)
  • argv[1]= "one"
  • argv[2] = "two"
  • argv[3] = "three"
  • argv[4] = NULL

argc is an integer that indicates the number of arguments in argv, and it's value is always greater or equal to 1. In the above example the value of argc will be 4.

Edited by Ancient Dragon

0

But when I am trying something like:
argv[1] < 48 || argv[1] > 57
It gives me such a warning: comparison between pointer and integer and it does not produce a correct result.

1

argv[1] is a pointer to a string, if you want the first character in the string then it's argv[1][0]

this makes more sense anyway
argv[1][0] < '0' || argv[1][0] > '9'

Edited by Ancient Dragon

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.