Modify a char* string in C

前端 未结 6 1323
既然无缘
既然无缘 2021-01-27 23:32

I have this:

char* original = \"html content\";

And want to insert a new

char* mycontent = \"newhtmlinsert\";

6条回答
  •  天命终不由人
    2021-01-27 23:48

    You need to use strcat() for this problem.

    Example =

    /* strcat example */
    #include 
    #include 
    
    int main ()
    {
      char str[80];
      strcpy (str,"these ");
      strcat (str,"strings ");
      strcat (str,"are ");
      strcat (str,"concatenated.");
      puts (str);
      return 0;
    }
    

    Though you need to check the bounds so you can use the bounds variant of strncat().

    #include 
    
    char *strncat(char *restrict s1, const char *restrict s2, size_t n);
    

    Make sure whatever buffer you are appending your string into has enough space to not cause a buffer overflow.

提交回复
热议问题