Hi! I'm constructing a code for parsing results in an html form and I'm having problems with strcat. It's really weird 'cause I'm positive the syntax and all that are all correct. I can't seem to find where the problem is.
Basically, I have this non-empty string "copy" and when I use:
strcat(copy,"test");
A server error occurs.
(test is just a test string to confirm if what I'm doing is correct)
But when I put the same code in a different part which should have no difference, the code is implemented correctly.
Here is the part of the code I'm having problems with:
(sorry if the code is kinda lousy)
if (data){
//MISCELLANEOUS CODE HERE
int check1 = 0,check2 = 0,check3 = 0;
char *copy = NULL,addressholder2;
copy = &addressholder2;
*copy = *string; //string is the result of the html form
copy[1] = '\0';
strcat(copy,"test"); //HERE IS THE CODE THAT WORKS
do{
//PARSES FIRST CHECKBOX RESULT
if (strncmp(string,"&Birthday wish=",strlen("&Birthday wish="))==0){
if (check1==0){
printf("<br>Birthday wish=");
check1++;
//strncat(copy,"Birthday wish=",14);
}
else if (check1!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Birthday wish=") - 1;
}
//PARSES SECOND CHECKBOX RESULT
else if (strncmp(string,"&Food=",strlen("&Food="))==0){
if (check2==0){
printf("<br>Food=");
check2++;
//strncat(copy,"Food=",5);
}
else if (check2!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Food=") - 1;
}
//PARSES THIRD CHECKBOX RESULT
else if (strncmp(string,"&Drinks=",strlen("&Drinks="))==0){
if (check3==0){
printf("<br>Drinks=");
check3++;
//strncat(copy,"Drinks=",7);
}
else if (check3!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Drinks=") - 1;
}
//REPLACE '&' WITH NEWLINE
else if (*string=='&'){
printf("<br>");
//strcat(copy,"\n");
}
else{
printf("%c",*string);
//strcat(copy,"test"); //HERE IS ONE OF THE LINES THAT DOESN'T WORK
}
string++;
}while(*string!='\0');
Basically if one of the strcat are not comments anymore, a server error occurs. I'm positive my algorithm here is correct, or have I missed something? I uploaded my .html file if you want to test it yourself. Thanks :)
The whole code is:
if (data){
//MISCELLANEOUS CODE HERE
int check1 = 0,check2 = 0,check3 = 0;
char *copy = NULL,addressholder2;
copy = &addressholder2;
*copy = *string; //string is the result of the html form
copy[1] = '\0';
strcat(copy,"test"); //HERE IS THE CODE THAT WORKS
do{
//PARSES FIRST CHECKBOX RESULT
if (strncmp(string,"&Birthday wish=",strlen("&Birthday wish="))==0){
if (check1==0){
printf("<br>Birthday wish=");
check1++;
//strncat(copy,"Birthday wish=",14);
}
else if (check1!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Birthday wish=") - 1;
}
//PARSES SECOND CHECKBOX RESULT
else if (strncmp(string,"&Food=",strlen("&Food="))==0){
if (check2==0){
printf("<br>Food=");
check2++;
//strncat(copy,"Food=",5);
}
else if (check2!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Food=") - 1;
}
//PARSES THIRD CHECKBOX RESULT
else if (strncmp(string,"&Drinks=",strlen("&Drinks="))==0){
if (check3==0){
printf("<br>Drinks=");
check3++;
//strncat(copy,"Drinks=",7);
}
else if (check3!=0){
printf(",");
//strncat(copy,",",1);
}
string = string + strlen("&Drinks=") - 1;
}
//REPLACE '&' WITH NEWLINE
else if (*string=='&'){
printf("<br>");
//strcat(copy,"\n");
}
else{
printf("%c",*string);
//strcat(copy,"test"); //HERE IS THE CODE THAT DOESN'T WORK
}
string++;
}while(*string!='\0');
}
printf("</body></html>");
return 0;
}