I’m having trouble with this…It compiles, but segfaults immediately when run…Using GDB, I determined that it segfaults while trying to initiate the long doubles. I feel like atoi might be the wrong function to use, but I tried other similar functions and still got a segfault.
int main(int argc, char *argv[]) {
long double x = atoi(argv[1]);
char oper = argv[2][0];
long double y = atoi(argv[3]);
Use
strtold:You can use the value of
*eto figure out whether any part of the string was consumed; see the manual for details. My example just checks whether the entire string could be parsed.I’m actually not sure if there’s a thread-safe solution…(Oh, I think it is, becauseerrnois thread-local.)