warning C6302: Format string mismatch: character string passed as parameter '4' when wide character

本文介绍了一个关于wchar与char类型不匹配导致的编译警告问题,并提供了使用安全函数wprintf_s和正确的格式说明符来解决此问题的方法。

LPCWSTR char2LPCWSTR(char* c_temp, WCHAR* temp /*= temp_wchar */)
{
 wmemset(temp, 0, MAX_PATH);

 swprintf_s(temp, MAX_PATH, L"%s", c_temp);

 return temp;
}

报了个warning  查msdn如下:

 

 

warning C6302: format string mismatch: character string passed as parameter <number> when wide character string is required in call to <function>

This warning indicates that the format string specifies that a wide character string is required. However, a character string is being passed. This defect is likely to cause a crash or a corruption of some form.

Example

The following sample code generates this warning because a character string is passed to wprintf function:

#include<stdio.h>
  
  void f()
  {
    char buff[5] = "hi";
  
    wprintf(L"%s", buff);
  }
  
#include<stdio.h>

void f()
{
  char buff[5] = "hi";

  wprintf(L"%s", buff);
}

The following sample code uses %hs to specify a single-byte character string with wprintf function:

#include<stdio.h>
  
  void f()
  {
    char buff[5] = "hi";
  
    wprintf(L"%hs", buff);
  }
  
#include<stdio.h>

void f()
{
  char buff[5] = "hi";

  wprintf(L"%hs", buff);
}

The following sample code uses safe string manipulation function wprintf_s to correct this warning:

#include<stdio.h>
  
  void f()
  {
    char buff[5] = "hi";
  
    wprintf_s(L"%hs", buff);
  }
  
 
总结 : 使用安全函数wprintf_s 指定格式为%hs就OK

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值