Getting unnecessary value while concatenating strings
I am getting problem while concatenating strings.I think this is the stupid question,but i didn't get answer.Thats why i am posting here.Please Help me.
The source code is
unsigned char arr_25[] = "{\"category\":\"",
arr_10[]="FMCG",
arr_26[]="\"amount\":",
arr_11[]="\",",
arr_12[]="1000",
arr_13[]="}";
float amount=1500,totamount=2500;
for(i=0;i<=count;i++)
{
if(i>0)
strcat(buff,",");
strcat(buff,arr_25);
strcat(buff,arr_10);//I am getting unnecessary value while printing this arr_10
strcat(buff,arr_11);
strcat(buff,arr_26);
memset(arr_12,0,sizeof(arr_12));
sprintf(arr_12,"%.2f",amount);
printf("\n the amount is %s \n",arr_12);
strcat(buff,arr_12);
strcat(buff,arr_13);
}
After 开发者_JAVA技巧printing the buff value i am getting
{"category":"FMCG","amount":1500.00},{"category":"FMCG00"amount":1500.00},{"category":"FMCG00"amount":1500.00},{"category":"FMCG00"amount":1500.00}
I am getting: "FMCG,FMCG00,FMCG00..."
Why does this occur? As i hard coded arr-10 value to FMCG..?
Thanks in Advance....
I don't know if it's the direct reason, but you are certainly overrunning arr_12
. You've defined it with size 5 and then you write "1500.00" into it.
you're probably overrunning some variables (arr_12?). Change sprintf with snprintf and you shouldn't have problems.
精彩评论