C6284

warning C6284: object passed as parameter '%d' when string is required in call to <function>.

This warning indicates that the format string specifies a string, for example, a %s specification for printf or scanf, but a C++ object has been passed instead.

This defect might produce incorrect output or crashes.

This message is often reported due to passing a C++ object implementing some string type, for example, std::string, CComBSTR or bstr_t, into a C printf-style call. Depending on the implementation of the C++ class, that is, if the proper cast operators are defined, C++ string objects can often be used transparently whenever C strings are required; however, because parameters to printf-style functions are essentially untyped, no conversion to a string occurs.

Depending on the object, it might be appropriate to insert a static_cast operator to the appropriate string type, for example, char * or TCHAR*, or to call a member function which returns a string, for example, c_str(), on instances of std::string.

Example

The following code generates this warning because a CComBSTR is passed to the sprintf function:

#include <atlbase.h>
#include <stdlib.h>

void f()
{
  char buff[50];
  CComBSTR bstrValue("Bye");
 
  sprintf(buff,"%ws",bstrValue); 
}

The following code uses static cast to correct this warning:

#include <atlbase.h>
#include <stdlib.h>

void f()
{
  char buff[50];
  CComBSTR bstrValue("Bye");
  
  sprintf_s(buff,50,"%ws",static_cast<wchar_t *>(bstrValue));
}

See Also

Reference

static_cast Operator

sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l