chanjianyi
chanjianyi

Reputation: 615

why the function strcat not working?

I have some code in C, I want to connect the ssid with the string "option" in the for loop

  void ApListCallback(ScanResult *pApList)
    {
        int i;

      printf("Find %d APs: \r\n", pApList->ApNum);

        for (i=0;i<pApList->ApNum;i++){
            char *ssid=pApList->ApList[i].ssid;
            char *temp=strcat(strcat("<option>",ssid),"</option>");
            printf("=======%s=======\r\n",ssid);
            printf("-------%s-------\r\n",temp);
            strcpy(ApListCallbackSelectStr, temp);
        }

        printf("---%s--\r\n",ApListCallbackSelectStr);
        }

and I get the result:

Find 11 APs:

=======MODIM FASHION=======
-------<option>-------
==============
-------<option>-------
=======360WiFi-6888=======
-------<option>-------
=======HAME_A5_037d=======
-------<option>-------
=======sweet baby=======
-------<option>-------
=======ringierguest=======
-------<option>-------
=======JIMMY 3G=======
-------<option>-------
=======MF70_9BC5E1=======
-------<option>-------
=======Bert-Co=======
-------<option>-------
---<option>--

why the function strcat not working?

Upvotes: 0

Views: 1089

Answers (5)

M.M
M.M

Reputation: 141648

The other answers show how to fix and still use strcat. However the much better way to write this code in C is:

printf("=======%s=======\n",ssid);
printf("-------<option>%s</option>-------\n", ssid);

You don't need to allocate memory or anything.

If you are also compiling every line into a string then you must also take care of the buffer size you are writing into. You have to protect against buffer overflow on ApListCallbackSelectStr. Here is one way to do it:

char *begin = ApListCallbackSelectStr;
char *const end = begin + sizeof ApListCallbackSelectStr;
// assuming `ApListCallbackSelectStr` is an array, not a pointer

for (i=0;i<pApList->ApNum;i++)
{    
    char const *ssid = pApList->ApList[i].ssid;
    int this = snprintf(begin, end - begin, 
                    "-------<option>%s</option>-------\n", ssid);

    if ( this < 0 || this >= end - begin )    // ran out of buffer, or other internal failure
        return; 

    printf("======%s======\n", ssid);
    printf("%*s\n", this, begin);

    begin += this;
}

Upvotes: 0

Andras
Andras

Reputation: 3055

if you look up the manpage for strcat, you'll see that the first argument should be a char[] buffer to hold the results. Unlike newer languages, strings in C are arrays of chars, and must be manipulated as such. Also, strcat only copies one argument, not a varags list. Try

char line[1000] = "";
strcat(line, "<option>");
strcat(line, ssid);
strcat(line, "</option>");
printf("%s\n", line);

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

"<option>" is readonly in the C language, so trying to write data to it, should actually cause a segmentation fault, you need a pointer to allocated memory to write into it, the following should work

void ApListCallback(ScanResult *pApList)
{
    int i;

    printf("Find %d APs: \r\n", pApList->ApNum);

    for (i=0;i<pApList->ApNum;i++)
    {
        char *ssid=pApList->ApList[i].ssid;
        /* reserve memory for the characters and point to it with temp */
        char *temp=malloc(strlen(ssid) + 18);
        if (temp != NULL)
        {
            /* copy the first part of the resulting string into temp */
            strcpy(temp, "<option>");
            /* append ssid to temp */
            strcat(temp, ssid);
            /* append the literal "</option>" to temp */
            strcat(temp, "</option>");
            strcpy(ApListCallbackSelectStr, temp);
            /* release the reserved memory */
            free(temp);
        }
    }

    printf("---%s--\r\n",ApListCallbackSelectStr);
}

the strlen function will return the number of characters in the string ssid, and the number of characters of <option></option> is 17, you also need an extra '\0' character that marks the end of the string, so total strlen(ssid) + 18.

Upvotes: 1

itsme86
itsme86

Reputation: 19526

You can't use a string literal as the first argument to strcat(). The first argument needs to contain enough space to contain the original string plus the concatenated string (plus a terminating zero).

You can read about strcat() here.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122493

char *temp=strcat(strcat("<option>",ssid),"</option>");

Here you are trying to concatenate to a string literal "<option>". The problem is: modifying string literals is undefined behavior.

Upvotes: 3

Related Questions