Quote:
Originally Posted by BW-userx
unsigned char and just char what I read is the same thing I googled it.
Code:
there is no difference. Actually on many platforms unqualified char is signed.
The same way -- e.g. if you have an 8-bit char, 7 bits can be used for magnitude
and 1 for sign. So an unsigned char might range from 0 to 255,
whilst a signed char might range from -128 to 127 (for example)
neither one is a digit so isdigit should return a non zero
|
But you missed the point, or I did not make it clear.
First,
isdigit() does not check the
type (i.e. signed, unsigned, int or char).
Code:
DESCRIPTION
These functions check whether c, which must have the value of an unsigned char or EOF,
falls into a certain character class according to the current locale.
It checks whether the encoded character represents a digit in the locale, usually ascii for our purposes.
My point was not
signed vs
unsigned char or
int, it was
char or
int, vs
pointer to char.
argv[optind] is always a pointer to a character array, so with...
Code:
isdigit(argv[optind])
...you are passing a pointer, not a char or an int.
char is defined as the
smallest representation of a character on any system, and pointer is always the
largest integer type. Hence the source of memory corruption and segfault.
This may also be obscured a bit if you are not including
ctype.h. Then
isdigit is probably inlined using a gcc macro so it results in no errors. Using
-Wall as previously mentioned may show a warning at compile time.
Next, when you call it this way, you are passing it an integer, that is a number, not a
digit encoded as a character.
Code:
isdigit(atoi(argv[optind]))
So it does not segfault, but it is no longer a valid test of the
character encoding and your test fails.
Dereferencing
argv[optind] passed the character (i.e. ascii value) and tests whether the
first character of the argument is an encoded digit...
Code:
isdigit(*argv[optind])
Note that it does not test the numeric value of multi-character arguments, only the
first character of the argument string. So if the argument is "
32", this only tests the "
3". If the argument is "
3X" it passes the test too!