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;
}

Recommended Answers

All 4 Replies

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

This is terrifying. So you have a pointer to a single character, and then immediately start writing beyond that single character as if it were an array to infinite memory. This code exhibits a profound misunderstanding of how pointers and memory work in C.

Assuming that you haven't done something equally wrong with string, it looks like you can use the length of string as a size for copy because it's pretty much guaranteed to be more than enough:

char *copy = malloc(strlen(string) + 1);

if (copy) {
    /* This may be overly paranoid, but handle an empty string */
    if (string[0] != '\0') {
        copy[0] = string[0];
        copy[1] = '\0';
    }
    else {
        copy[0] = '\0';
    }

    /* Now do all of your stuff */
    strcat(copy, "test");

    ...
}

Also, don't forget to release the memory for copy by calling free() at a suitable location (ie. when you're done using the memory).

Oh I get it. That's why I can't seem to find an error because I never realized that the pointers could have a conflict in memory. Thanks a lot! :D Now I can move forward with what I'm doing :) Oh, one last question. If "copy" has, say, "Name" and I were to do this:

strcat(copy,"\nAge");

Would "\n" be stored inside the same address so that it still functions as a new line or not since I'm using this as a .cgi file connected to an html file and only <br> works? Because I have to save "copy" in a text file. Thanks for your help! :)

Would "\n" be stored inside the same address so that it still functions as a new line or not since I'm using this as a .cgi file connected to an html file and only <br> works? Because I have to save "copy" in a text file.

If it's going into a text file then '\n' will act as a newline. If it's being rendered by a browser, I'm not 100% sure whether the CGI interpreter will convert that to an HTML line break or not. That's something you'd need to test and adjust accordingly. I actually haven't worked with CGI in about 10 years, so the details are fuzzy. ;)

Okay :) Thanks a lot for your help! Finally, almost finished with my code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.