Search code examples
c++variablesreturn

Store return size * nmemb into a variable c++


How would I go about storing the return size * nmemb? I've tried std::string verycool = size * nmemb;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

Solution

  • Just use the same type that size and nmemb themselves are using:

    static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
    {
        size_t numBytes = size * nmemb;
        ((std::string*)userp)->append((char*)contents, numBytes);
        return numBytes;
    }
    

    If, on the other hand, you want to access the value after WriteCallback() exits, just use the string::size() method for that, eg:

    std::string s;
    functionThatSetsUserP(&s);
    functionThatWrites(&WriteCallback);
    size_t size = s.size();