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