In deze tutorial leer je de functie strcpy () in C-programmering gebruiken om strings te kopiëren (met behulp van een voorbeeld).
C strcpy ()
Het functieprototype van strcpy()
is:
char* strcpy(char* destination, const char* source);
- De
strcpy()
functie kopieert de string die is aangewezen door source (inclusief het null-teken) naar de bestemming. - De
strcpy()
functie retourneert ook de gekopieerde string.
De strcpy()
functie is gedefinieerd in het header-bestand string.h.
Voorbeeld: C strcpy ()
#include #include int main() ( char str1(20) = "C programming"; char str2(20); // copying str1 to str2 strcpy(str2, str1); puts(str2); // C programming return 0; )
Uitvoer
C programmeren
Opmerking: wanneer u gebruikt strcpy()
, moet de grootte van de bestemmingsreeks groot genoeg zijn om de gekopieerde reeks op te slaan. Anders kan dit leiden tot ongedefinieerd gedrag .