1STRCPY(3P) POSIX Programmer's Manual STRCPY(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 stpcpy, strcpy — copy a string and return a pointer to the end of the
13 result
14
16 #include <string.h>
17
18 char *stpcpy(char *restrict s1, const char *restrict s2);
19 char *strcpy(char *restrict s1, const char *restrict s2);
20
22 For strcpy(): The functionality described on this reference page is
23 aligned with the ISO C standard. Any conflict between the requirements
24 described here and the ISO C standard is unintentional. This volume of
25 POSIX.1‐2017 defers to the ISO C standard.
26
27 The stpcpy() and strcpy() functions shall copy the string pointed to by
28 s2 (including the terminating NUL character) into the array pointed to
29 by s1.
30
31 If copying takes place between objects that overlap, the behavior is
32 undefined.
33
35 The stpcpy() function shall return a pointer to the terminating NUL
36 character copied into the s1 buffer.
37
38 The strcpy() function shall return s1.
39
40 No return values are reserved to indicate an error.
41
43 No errors are defined.
44
45 The following sections are informative.
46
48 Construction of a Multi-Part Message in a Single Buffer
49 #include <string.h>
50 #include <stdio.h>
51
52 int
53 main (void)
54 {
55 char buffer [10];
56 char *name = buffer;
57
58 name = stpcpy (stpcpy (stpcpy (name, "ice"),"-"), "cream");
59 puts (buffer);
60 return 0;
61 }
62
63 Initializing a String
64 The following example copies the string "----------" into the
65 permstring variable.
66
67
68 #include <string.h>
69 ...
70 static char permstring[11];
71 ...
72 strcpy(permstring, "----------");
73 ...
74
75 Storing a Key and Data
76 The following example allocates space for a key using malloc() then
77 uses strcpy() to place the key there. Then it allocates space for data
78 using malloc(), and uses strcpy() to place data there. (The user-
79 defined function dbfree() frees memory previously allocated to an array
80 of type struct element *.)
81
82
83 #include <string.h>
84 #include <stdlib.h>
85 #include <stdio.h>
86 ...
87 /* Structure used to read data and store it. */
88 struct element {
89 char *key;
90 char *data;
91 };
92
93 struct element *tbl, *curtbl;
94 char *key, *data;
95 int count;
96 ...
97 void dbfree(struct element *, int);
98 ...
99 if ((curtbl->key = malloc(strlen(key) + 1)) == NULL) {
100 perror("malloc"); dbfree(tbl, count); return NULL;
101 }
102 strcpy(curtbl->key, key);
103
104 if ((curtbl->data = malloc(strlen(data) + 1)) == NULL) {
105 perror("malloc"); free(curtbl->key); dbfree(tbl, count); return NULL;
106 }
107 strcpy(curtbl->data, data);
108 ...
109
111 Character movement is performed differently in different implementa‐
112 tions. Thus, overlapping moves may yield surprises.
113
114 This version is aligned with the ISO C standard; this does not affect
115 compatibility with XPG3 applications. Reliable error detection by this
116 function was never guaranteed.
117
119 None.
120
122 None.
123
125 strncpy(), wcscpy()
126
127 The Base Definitions volume of POSIX.1‐2017, <string.h>
128
130 Portions of this text are reprinted and reproduced in electronic form
131 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
132 table Operating System Interface (POSIX), The Open Group Base Specifi‐
133 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
134 Electrical and Electronics Engineers, Inc and The Open Group. In the
135 event of any discrepancy between this version and the original IEEE and
136 The Open Group Standard, the original IEEE and The Open Group Standard
137 is the referee document. The original Standard can be obtained online
138 at http://www.opengroup.org/unix/online.html .
139
140 Any typographical or formatting errors that appear in this page are
141 most likely to have been introduced during the conversion of the source
142 files to man page format. To report such errors, see https://www.ker‐
143 nel.org/doc/man-pages/reporting_bugs.html .
144
145
146
147IEEE/The Open Group 2017 STRCPY(3P)