C言語 文字列コピーの基本とstrcpy関数
C言語では、strcpy()関数を使って文字列を別の文字列にコピーすることができます。この関数はstring.hヘッダーファイルで定義されており、そのプロトタイプは以下のようになります。
char *strcpy(char *dest, const char *src);
dest は目標文字列であり、src はソース文字列です。以下は簡単な例です:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[100];
strcpy(dest, src);
printf("Source string: %s\n", src);
printf("Destination string: %s\n", dest);
return 0;
}
上記のコードを実行すると、出力は次の通りになります:
Source string: Hello, World!
Destination string: Hello, World!