...
Code Block | ||
---|---|---|
| ||
char *copy(size_t n, const char *str) { int i; char *p; if (n == 0) { /* Handle unreasonable object size error */ } p = (char *)malloc(n); if (p == NULL) { /* Handle malloc failure */ } for ( i = 0; i < n; ++i ) { p[i] = *str++; } return p; } /* ... */ char str[] = "hi there"; char *p = copy(sizeof(str), str); |
...
Code Block | ||
---|---|---|
| ||
char *copy(rsize_t n, const char *str) {
rsize_t i;
char *p;
if (n == 0 || n > RSIZE_MAX) {
/* Handle unreasonable object size error */
}
p = (char *)malloc(n);
if (p == NULL) {
/* Handle malloc failure */
}
for ( i = 0; i < n; ++i ) {
p[i] = *str++;
}
return p;
}
/* ... */
char str[] = "hi there";
char *p = copy(sizeof(str), str);
|
...