int sprintf_s(char *,size_t,const char *,...)”: 不能將參數 2 從“const char [3]”轉換為“size_t”


2014-03-02 20:14
在編譯下列代碼時,出現以下錯誤: 
cpp(23) : error C2664: “int sprintf_s(char *,size_t,const char *,...)”: 不能將參數 2 從“const char [3]”轉換為“size_t” 
請問這是什么意思?該怎么修改? 
#include<iostream> 
#include<string> 
#include<stdio.h> 
using namespace std; 
string dec_to_hex(double ); 
int main(){ 
double x; 
string str; 
cout<<"please input a decimal number:\n"; 
cin>>x; 
str=dec_to_hex(x); 
cout<<str; 

string dec_to_hex(double x){ 
char* ch[20]; 
string str,str1; 
int n=int(x),num,count; 
double m=x-n; 
while(n){ 
count=0; 
num=n%16; 
n=n/16; 
    sprintf_s(ch[count],"%x",num); 
count++; 

for(unsigned i=count;1>0;i--){ 
str.append(ch[i-1]); 

    cout<<str<<endl; 
return str; 










 

回復討論(解決方案)

sprintf_s(ch[count],1,"%x",num);
第二個參數是長度 無符號int型
sprintf_s()是sprintf()的安全版本,通過指定緩沖區長度來避免sprintf()存在的溢出風險 
即第二個參數為size_t,指緩沖區的最大長度
sprintf_s()是sprintf()的安全版本,通過指定緩沖區長度來避免sprintf()存在的溢出風險 
即第二個參數為size_t,指緩沖區的最大長度 
+1
知道了!謝謝各位!
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM