Search code examples
cstringstring-parsing

strtoll/_strtoi64 Parameter Issue


I am having trouble understanding this code:

static long long _be_decode_int(const char **data, long long *data_len)
{
    char *endp;
    long long ret = strtoll(*data, &endp, 10);
    *data_len -= (endp - *data);
    *data = endp;
    return ret;
}

I have changed strtoll to _strtoi64 because I am programming on Windows and believe them to perform the same function.

According to the MSDN page for _strtoi64, the second parameter should be a pointer to the character that ends the string. If endp has only just been declared, what does it point to?


Solution

  • It's a "result parameter" -- you don't have to put anything into the pointer, and after the function returns it will point to the character after the number.

    EDIT: this is also why you are passing &endp and not just endp -- the function needs a "pointer to the pointer" so it can fill in the pointer value