What is the best way to return an error from a function when I'm already returning a value?

前端 未结 8 1903
盖世英雄少女心
盖世英雄少女心 2020-12-13 13:42

I wrote a function in C that converts a string to an integer and returns the integer. When I call the function I also want it to let me know if the string is not a valid num

8条回答
  •  遥遥无期
    2020-12-13 13:55

    Take a look at how the standard library deals with this problem:

    long  strtol(const  char  * restrict str,  char **restrict endptr, int base);
    

    Here, after the call the endptr points at the first character that could not be parsed. If endptr == str, then no characters were converted, and this is a problem.

提交回复
热议问题