I need to write a function in C that converts a string (data pointed to by a char *) to an int. I've already done this successfully with this code:
int charTOint(char * c) {
char p = *c;
int ergebnis = 0;
while (p) {
ergebnis = ergebnis * 10 + (p - '0');
c++;
p = *c;
}
return ergebnis;
}
My problem now is, that I need it to also work with negative numbers / char arrays starting with a '-'.
I know that I have to check, whether the first char is a '-', but I'm stuck after that.
I know the '-' is the 45th character of the ASCII table, but I somehow can't think of a way to make it work.