C言語で文字列をコピーする方法は何ですか。
C言語では、文字列をコピーするためにstrcpy関数を使用することができます。strcpy関数のプロトタイプは以下の通りです:
char* strcpy(char* destination, const char* source);
その中で、destinationは目標の文字列を示し、sourceは元の文字列を示します。
以下はサンプルです:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
結果は次のようになります:
Source: Hello, World!
Destination: Hello, World!
strcpy関数を使用する際には、バッファオーバーフローを避けるために、目的の文字列のバッファが十分に大きいことを確認してください。