Thursday, July 30, 2009

C question?

If abc is a string, line[i] is a character, how can I add line[i] to the end of string abc? it has a warning that says casting, so I'm guessing it would output something wrong too. Please help.





strcat(abc, line[i]);

C question?
Your code would lead to unpredictable behavior. Both parameters to strcat() are supposed to be strings (or more accurately, pointers to a character). The correct way to pass your character to the function would be like this:


strncat(abc, %26amp;line[i], 1);
Reply:The prototype of strcat is strcat(string1, string2)





and you are trying to do strcat(string, char)





Do it like this





char myarray[1]={line[i], '\0'};





strcat(abc, myarray);
Reply:char [] str1 = {'a','b','c'};





// since str1 is of size 3 you can't append anything to it.


so we will create an array of 4 and will copy strngs to it





char str2[4];


strcpy(str2,str1);


strcpy(str2[2],len[i],1);
Reply:strncat(abc,line+i,1);


No comments:

Post a Comment