Liste des Groupes | Revenir à cl misc |
>Something which would match [0-9]+ in its first argument (if any) would>
be:
>
#include "string.h"
#include "stdlib.h"
>
int main(int argc, char **argv)
{
char *p;
unsigned c;
>
p = argv[1];
if (!p) exit(1);
while (c = *p, c && c - '0' > 10) ++p;
if (!c) exit(1);
return 0;
}
>
but that's 14 lines of text, 13 of which have absolutely no relation to
the problem of recognizing a digit.
This is wrong in many ways. Did you actually test that program?
>
First of all, why `"string.h"` and not `<string.h>`? Ok, that's
not technically an error, but it's certainly unconventional, and
raises questions that are ultimately a distraction.
Such as your paragraph above.
>Second, suppose that `argc==0` (yes, this can happen under>
POSIX).
It can happen in case of some piece of functionally hostile software
intentionally creating such a situation. Tangential, irrelevant
point. If you break it, you get to keep the parts.
>Third, the loop: why `> 10`? Don't you mean `< 10`? You are>
trying to match digits, not non-digits.
Mistake I made. The opposite of < 10 is > 9.
Fourth, you exit with failure (`exit(1)`) if `!p` *and* if `!c`>
at the end, but `!c` there means you've reached the end of the
string; which should be success.
Mistake you made: [0-9]+ matches if there's at least one digit in the
string. That's why the loop terminates once one was found. In this case,
c cannot be 0.
Les messages affichés proviennent d'usenet.