Strcpy

Strcpy

The C programming language offers a library function called strcpy, defined in the string.h header file, that allows null-terminated memory blocks to be copied from one location to another. Since strings in C are not first-class data types and are implemented instead as contiguous blocks of bytes in memory, strcpy will effectively copy strings given two pointers to blocks of allocated memory.

The prototype of the function is: [ [http://www.cplusplus.com/reference/clibrary/cstring/strcpy.html strcpy - C++ Reference ] ] char *strcpy(char *destination, const char *source);The return value is destination.

Usage and implementation

For examplechar *str1 = malloc(LARGE_NUMBER);char *str2 = malloc(LARGE_NUMBER);if(str1 && str2) { if(fgets(str1, LARGE_NUMBER, stdin)) strcpy(str2, str1); /* the argument order mimics that of an assignment: str2 "=" str1 */}free(str1);free(str2);In the first two lines memory is allocated to hold two strings storing the memory addresses in str1 and str2. Next the memory pointed by str1 is filled using some user-input string. After that the string is copied from one memory block into the other. Although the simple assignment str2 = str1 might appear to do the same thing, it only copies the memory address of str1 into str2 but not the actual string. Both str1 and str2 would refer to the same memory block. This is known as a shallow copy because it does not actually create a new, identical string.

The strcpy function performs a copy by iterating over the individual characters of the string and copying them one by one. An explicit implementation of strcpy (there are more compact possibilities) is:

char *strcpy(char *dest, const char *src){ const char *p; char *q; for(p = src, q = dest; *p != '


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Strcpy — функция стандартной библиотеки языка программирования Си, для копирования нуль терминированной строки в заданный буфер. Содержание 1 Прототип функции 2 Возвращаемое значение …   Википедия

  • strcpy — strcpy  функция стандартной библиотеки языка программирования Си, для копирования нуль терминированной строки (включая нуль терминатор) в заданный буфер . Содержание 1 Прототип функции 2 Возвращаемое значение …   Википедия

  • Strcpy — Función incluida dentro del archivo de cabecera de constantes string.h del lenguaje de programación C. Contenido 1 Prototipo 2 Parámetros 3 Descripción 4 Ejemplo …   Wikipedia Español

  • Buffer Overflow — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Buffer overflow — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Depassement de tampon — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Débordement de buffer — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Débordement de tampon — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Débordements de buffer — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Dépassement De Tampon — En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon, écrasant ainsi des… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”