C6273

警告 6273:传递了非整数参数 <数字>,而对 <函数> 的调用需要整数参数:如果当前传递的是一个指针值,应使用 %p

此警告意味着,格式字符串指定需要整数,例如,为 printf 指定 %d、长度或优先级,但所传递的不是整数参数,如 float、字符串或 struct。此缺陷可能导致错误的输出。

示例

在下面的代码中,因为 sprintf 函数需要整数而非 float ,所以会生成此警告:

#include <stdio.h>
#include <string.h>

void f_defective()
{
  char buff[50];
  float f=1.5;
  
  sprintf(buff, "%d",f);
}

下面的代码使用整型强制转换来更正此警告:

#include <stdio.h>
#include <string.h>

void f_corrected()
{
  char buff[50];
  float f=1.5;

  sprintf(buff,"%d",(int)f);
}

下面的代码使用安全的字符串操作函数 sprintf_s 来更正此警告:

#include <stdio.h>
#include <string.h>

void f_safe()
{
  char buff[50];
  float f=1.5;

  sprintf_s(buff,50,"%d",(int)f);
}

此警告不适用于 Windows 9x 和 Windows NT 4 版,因为这些平台不支持 %p。

请参见

参考

sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l