1 2 3 4 5 6 7 8 9 10

Of course we don't really need this return(tmp),but it does help us handle the error condition where malloc fails.

In the above we have assumed that malloc always succeeds. strcpy() must handle failure by returning the NULL pointer.

tmp = (char*) malloc (1+strlen(s));

if ( tmp == NULL) return NULL;

for ( i = 0; *(s+i) != '\0'; i++ )
*(tmp+i) = *(s+i);
return (tmp);

Outside in the calling program one might well actually use strcpy as follows:

if (strcpy (tmp, s) != NULL) {.........}

4 February, 1998

7

Copyright University of Alberta