Chapter Contents

Previous

Next
strcpy

strcpy



Copy a Null-Terminated String

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

char *strcpy(char *to, const char *from);


DESCRIPTION

strcpy copies characters from the second argument string, from , to the first argument string, to , until a terminating-null character is found. The null also is copied.


RETURN VALUE

The return value is a pointer to the to string.


CAUTION

No check is made (or can be made) to see if there is room in the to string for all the characters of the from string. Characters are copied until a null character is found, or until a protection or addressing exception occurs.

The effect of strcpy is not defined if the to and from fields overlap.


IMPLEMENTATION

Provided that <string.h> is included (by an #include statement) and strcpy is not undefined (by an #undef statement), strcpy is implemented by inline code.


EXAMPLE

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define WORDSIZE 20

main()
{
   char *text = "Some of this "line" is in "quotes".";
   char *quotepos1, *quotepos2;
   char word[WORDSIZE];

   printf("The input text is:\n%s\n", text);
   quotepos1 = strchr(text, '"');
   if (quotepos1 == NULL) {
      puts("There are no quotes in this line.");
      abort();
   }
   quotepos2 = strchr(quotepos1+1, '"');
   if (quotepos2 == NULL) {
      puts("There is only one quotation mark in this line.");
      abort();
   }
   if (quotepos2 - quotepos1 > WORDSIZE)
      puts("The first word in quotes is too large to handle.");
   else {
      strcpy(word, quotepos1+1);          /* Copy the word.           */
      word[quotepos2-quotepos1-1] = '\0'; /* Null-terminate the word. */
      printf("The first word in quotation marks in the text is "%s".",
             word);
   }
}


RELATED FUNCTIONS

memcpy , strcat , strncpy , strsave


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.