回覆列表
  • 1 # 火奇飛

    函式1:char * strcat ( char * destination, const char * source );

    /* strcat example */

    #include <stdio.h>

    #include <string.h>

    int main () {

    char str[80];

    strcpy (str,"these ");

    strcat (str,"strings ");

    strcat (str,"are ");

    strcat (str,"concatenated.");

    puts (str); return 0;

    }

    輸出是:these strings are concatenated.

    這個函式的問題是不夠安全,合併後的字串長度可能超過destination預分配的空間,造成記憶體越界。

    函式2:int snprintf( char *buffer, int buff_size, const char *format, ... );

    /* snprintf example */

    #include <stdio.h>

    #include <string.h>

    int main () {

    char str[10];

    strcpy (str,"these ");

    snprintf(str+strlen(str),sizeof(str)-strlen(str),"%s", "strings ");

    puts (str);

    return 0;

    }

    輸出是:these stri

  • 中秋節和大豐收的關聯?
  • 你看過哪些最雷人、最搞笑、最不可思議的買家秀?