C++ Why does sprintf_s format change when used with void pointer?

c++, printf

Solution

This happens because `sizeof(void *) == 4` in your case. And you implicitly cast `__int64` to `void *` by function call. So if you use `%llu` format, you print some garbage from memory.

I suggest you to rewrite function if it is possible:

template <typename T>
void addBuffered(T *attributeValue, char* format)
{
    sprintf_s(buffer, sizeof(buffer), format, *attributeValue);
}

call example:

addBuffered(&num, "%luu");

Problem

This is the working original code: ``` // ... unsigned __int64 num = 57; sprintf_s(buffer, sizeof(buffer), "%llu", num); ``` However, when I try to extract this part into this function: ``` void addBuffered(void** attributeValue, char* format) { sprintf_s(buffer, sizeof(buffer), format, *attributeValue); } ``` by calling: ``` addBuffered((void**)&num, "%d"); ``` I have to change the format parameter in `sprintf_s` from `%llu` to `%d` to get the correct value. Can somebody explain why this happens and if the change of the parameter to `%d` can be a problem? Thanks!

Original source