char - C++ Using function with sprintf_s inside sprintf_s -


i want write data char buffer. therefore have function format data. example, pass int value function this:

using namespace std; #include <iostream>  char* writedata(int idata, char* sbufferdata){     sprintf_s(sbufferdata, 2048, "val:%i", idata);     return sbufferdata; }  int _tmain(int argc, _tchar* argv[]) {      char sbuffer[2048];     char sbufferdata[2048];      sprintf_s(sbuffer, 2048, "%s, %s",         writedata(1, sbufferdata),         writedata(2, sbufferdata)         );     printf(sbuffer);      cin.get();      return 0; } 

but output result is: val:1, val:1

what have is: val:1, val:2

can explain me, why 2 times 1 value in final buffer?

char sbuffer[2048]; char sbufferdata[2048]; sprintf_s(sbuffer, 2048, "%s, %s",     writedata(1, sbufferdata),     writedata(2, sbufferdata)     ); 

you reusing sbufferdata both calls of writedata. 1 call function overwrites result of other call before around sprintf_s.

add separate buffer:

char sbuffer[2048]; char sbufferdata1[2048]; char sbufferdata2[2048]; sprintf_s(sbuffer, 2048, "%s, %s",     writedata(1, sbufferdata1),     writedata(2, sbufferdata2)     ); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -