2014-03-02 20:14The following error occurred while compiling the following code:
CPP: Error C2664: "int sprintf_s (char *,size_t,const char *,...)": cannot convert parameter 2 from "const char [3]" to "size_t"
What does this mean, please? How do I change it?
#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;
}
Reply to discussion (solution) sprintf_s (ch[count],1, "%x", num), the second parameter is a secure version of the length unsigned int type sprintf_s () is sprintf (), avoiding sprintf by specifying the buffer length () Presence of overflow risk
The second parameter is size_t, which means that the maximum length of the buffer sprintf_s () is the secure version of sprintf () and avoids the risk of overflow by specifying the buffer length to avoid sprintf ()
The second parameter is size_t, which refers to the maximum length of the buffer
+1 got it! Thank you, everyone! Transferred from: http://www.itnose.net/detail/973571.html
int sprintf_s (char *,size_t,const char *,...) ": cannot convert parameter 2 from" const char [3] "to" size_t "