1STRCPY(P) POSIX Programmer's Manual STRCPY(P)
2
3
4
6 strcpy - copy a string
7
9 #include <string.h>
10
11 char *strcpy(char *restrict s1, const char *restrict s2);
12
13
15 The strcpy() function shall copy the string pointed to by s2 (including
16 the terminating null byte) into the array pointed to by s1. If copying
17 takes place between objects that overlap, the behavior is undefined.
18
20 The strcpy() function shall return s1; no return value is reserved to
21 indicate an error.
22
24 No errors are defined.
25
26 The following sections are informative.
27
29 Initializing a String
30 The following example copies the string "----------" into the
31 permstring variable.
32
33
34 #include <string.h>
35 ...
36 static char permstring[11];
37 ...
38 strcpy(permstring, "----------");
39 ...
40
41 Storing a Key and Data
42 The following example allocates space for a key using malloc() then
43 uses strcpy() to place the key there. Then it allocates space for data
44 using malloc(), and uses strcpy() to place data there. (The user-
45 defined function dbfree() frees memory previously allocated to an array
46 of type struct element *.)
47
48
49 #include <string.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 ...
53 /* Structure used to read data and store it. */
54 struct element {
55 char *key;
56 char *data;
57 };
58
59
60 struct element *tbl, *curtbl;
61 char *key, *data;
62 int count;
63 ...
64 void dbfree(struct element *, int);
65 ...
66 if ((curtbl->key = malloc(strlen(key) + 1)) == NULL) {
67 perror("malloc"); dbfree(tbl, count); return NULL;
68 }
69 strcpy(curtbl->key, key);
70
71
72 if ((curtbl->data = malloc(strlen(data) + 1)) == NULL) {
73 perror("malloc"); free(curtbl->key); dbfree(tbl, count); return NULL;
74 }
75 strcpy(curtbl->data, data);
76 ...
77
79 Character movement is performed differently in different implementa‐
80 tions. Thus, overlapping moves may yield surprises.
81
82 This issue is aligned with the ISO C standard; this does not affect
83 compatibility with XPG3 applications. Reliable error detection by this
84 function was never guaranteed.
85
87 None.
88
90 None.
91
93 strncpy() , the Base Definitions volume of IEEE Std 1003.1-2001,
94 <string.h>
95
97 Portions of this text are reprinted and reproduced in electronic form
98 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
99 -- Portable Operating System Interface (POSIX), The Open Group Base
100 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
101 Electrical and Electronics Engineers, Inc and The Open Group. In the
102 event of any discrepancy between this version and the original IEEE and
103 The Open Group Standard, the original IEEE and The Open Group Standard
104 is the referee document. The original Standard can be obtained online
105 at http://www.opengroup.org/unix/online.html .
106
107
108
109IEEE/The Open Group 2003 STRCPY(P)