How do I concatenate const/literal strings in C?

前端 未结 17 1493
醉梦人生
醉梦人生 2020-11-21 23:45

I\'m working in C, and I have to concatenate a few things.

Right now I have this:

message = strcat(\"TEXT \", var);

message2 = strcat(strcat(\"TEXT          


        
相关标签:
17条回答
  • 2020-11-22 00:13

    Try something similar to this:

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, const char * argv[])
    {
      // Insert code here...
      char firstname[100], secondname[100];
      printf("Enter First Name: ");
      fgets(firstname, 100, stdin);
      printf("Enter Second Name: ");
      fgets(secondname,100,stdin);
      firstname[strlen(firstname)-1]= '\0';
      printf("fullname is %s %s", firstname, secondname);
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 00:14

    Folks, use strncpy(), strncat(), or snprintf().
    Exceeding your buffer space will trash whatever else follows in memory!
    (And remember to allow space for the trailing null '\0' character!)

    0 讨论(0)
  • 2020-11-22 00:14

    You are trying to copy a string into an address that is statically allocated. You need to cat into a buffer.

    Specifically:

    ...snip...

    destination

    Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
    

    ...snip...

    http://www.cplusplus.com/reference/clibrary/cstring/strcat.html

    There's an example here as well.

    0 讨论(0)
  • 2020-11-22 00:15

    Best way to do it without having a limited buffer size is by using asprintf()

    char* concat(const char* str1, const char* str2)
    {
        char* result;
        asprintf(&result, "%s%s", str1, str2);
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 00:17

    Avoid using strcat in C code. The cleanest and, most importantly, the safest way is to use snprintf:

    char buf[256];
    snprintf(buf, sizeof buf, "%s%s%s%s", str1, str2, str3, str4);
    

    Some commenters raised an issue that the number of arguments may not match the format string and the code will still compile, but most compilers already issue a warning if this is the case.

    0 讨论(0)
  • 2020-11-22 00:21

    If you have experience in C you will notice that strings are only char arrays where the last character is a null character.

    Now that is quite inconvenient as you have to find the last character in order to append something. strcat will do that for you.

    So strcat searches through the first argument for a null character. Then it will replace this with the second argument's content (until that ends in a null).

    Now let's go through your code:

    message = strcat("TEXT " + var);
    

    Here you are adding something to the pointer to the text "TEXT" (the type of "TEXT" is const char*. A pointer.).

    That will usually not work. Also modifying the "TEXT" array will not work as it is usually placed in a constant segment.

    message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
    

    That might work better, except that you are again trying to modify static texts. strcat is not allocating new memory for the result.

    I would propose to do something like this instead:

    sprintf(message2, "TEXT %s TEXT %s", foo, bar);
    

    Read the documentation of sprintf to check for it's options.

    And now an important point:

    Ensure that the buffer has enough space to hold the text AND the null character. There are a couple of functions that can help you, e.g., strncat and special versions of printf that allocate the buffer for you. Not ensuring the buffer size will lead to memory corruption and remotely exploitable bugs.

    0 讨论(0)
提交回复
热议问题