Edit

Share via


Warning C6067

Parameter 'number' in call to 'function' must be the address of the string

Remarks

This warning indicates a mismatch between the format specifier and the function parameter. Even though the warning suggests using the address of the string, you must check the type of parameter a function expects before correcting the problem. For example, a %s specification for printf requires a string argument, but a %s specification in scanf requires an address of the string.

This defect is likely to cause a crash or corruption of some form.

Code analysis name: NON_STRING_ARGUMENT_TO_FORMAT_FUNCTION

Example

The following code generates this warning because an integer is passed instead of a string:

#include <stdio.h>

void f_defective()
{
  char *str = "Hello, World!";
  printf("String:\n %s", 1);
  // code ...
}

To correct the warning, pass a string as a parameter to printf as shown in the following code:

#include <stdio.h>

void f_corrected()
{
  char *str = "Hello, World!";
  printf("String:\n %s", str);
  // code ...
}

The following code generates this warning because an incorrect level of indirection is specified when passing the parameter, buffer, to scanf:

#include <stdio.h>

void h_defective()
{
  int retval;
  char* buffer = new char(20);
  if (buffer)
  {
    retval = scanf("%s", &buffer); // warning C6067
    // code...
    delete buffer;
  }
}

To correct above warnings, pass the correct parameter as shown in the following code:

#include <stdio.h>

void h_corrected()
{
  int retval;
  char* buffer = new char(20);
  if (buffer)
  {
    retval = scanf("%s", buffer);
    // code...
    delete buffer;
  }
}

The following code uses safe string manipulation functions to correct this warning:

#include <stdio.h>

void f_safe()
{
  char buff[20];
  int retVal;

  sprintf_s(buff, 20, "%s %s", "Hello", "World!");
  printf_s("String:\n   %s  %s", "Hello", "World!");
  retVal = scanf_s("%s", buff, 20);
}

See also

Format specification syntax: printf and wprintf functions
sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
printf, _printf_l, wprintf, _wprintf_l
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
C4313
C4477