How do I tell if the c function atoi failed or if it was a string of zeros?

后端 未结 6 1462
悲&欢浪女
悲&欢浪女 2020-11-28 09:52

When using the function atoi (or strtol or similar functions for that matter), how can you tell if the integer conversion failed or if the C-string

6条回答
  •  一生所求
    2020-11-28 10:10

    From the man page for strtol():

    If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtol() stores the original value of nptr in *endptr. (Thus, if *nptr is not '\0' but **endptr is '\0' on return, the entire string was valid.)

提交回复
热议问题