Confused how to convert from a string to double using strtod() in C++

前端 未结 3 876
心在旅途
心在旅途 2021-01-14 11:08

If someone could explain how to use the function, that would be great. I don\'t understand the parameters.

Thanks

3条回答
  •  一向
    一向 (楼主)
    2021-01-14 11:46

    The first argument is a the string you want to convert, the second argument is a reference to a char* that you want to point to the first char after the float in your original string (in case you want to start reading the string after the number). If you do not care about the second argument, you can set it to NULL.

    For example, if we have the following variables:

    char* foo = "3.14 is the value of pi"
    float pi;
    char* after;
    

    After pi = strtod(foo, after) the values are going to be:

    foo is "3.14 is the value of pi"
    pi is 3.14f
    after is " is the value of pi"
    

    Note that both foo and after are pointing to the same array.

提交回复
热议问题