Re: is_numeric Function In C
Firstly, you declared your function to return an int, but you are actually returning a pointer. I suggest you return a 1 or a 0, as that i think would be most intuitive. Alternativly you could make the function return a pointer to char (char *).
Secondly, this line:
for(c = 0; &input[c] != "\0";c++)
has a problem. You're compareing the address of the ith element with a string. What you should do is change it to this:
for(c = 0; input[c] != '\0';c++)
Finally, because you've got your final if statement in your loop, as long as the first character is numeric it will return.
What you should do is remove the else return part, and at the end of the function assume that it's numeric (since there are no other characters in it).
Re: is_numeric Function In C
The comparison input[c] < '0' && input[c] > '9' is is not right, this expression is never true, you should use >='0' && <='9' if you mean "between 0 and 9".
While it is not an error, the ok variable is not needed here. You could simply use return statements:
Code:
int is_numeric(char input[256])
{
int c;
for(c = 0; input[c] != "\0";c++)
{
if(input[c] < '0' || input[c] > '9')
{
return 0;
}
}
return 1;
}