strcpy function


strcpy copies a string. This function will copy the bytes stored at the location pointed to by 's2' to the location pointed to by 's1'.


     	s1		s2
	|		|
	V		V
        - - - -		- - - --	
       | | | | |       |a|b|c|\0|
        - - - -		- - - --	
	^ ^		| |
	| |		| |
	 -|-------------  |
	   ---------------

Library:   string.h

Prototype: char strcpy(char *s1, const char *s2);

Syntax:	  
	   char string2[20]="red dwarf";
	   char string1[20]="";
           strcpy(string1, string2);

Notes

Dont forget that strings are terminated with a '\0' so allow space for it...

There is another way to code the example above. Consider this piece of code.

	main()
	{
	  char *string2="red dwarf";
	  char *string1;

	  string1=string2;
	}
'string2' is now a character pointer (only one byte) that points to a storage location containing "red dwarf" (a string constant). So string1=string2; copies the address of "red dwarf" into 'string1'. This version of the code will execute quicker than strcpy because less data is being moved around the system.
example program.

See also:

strtok
strncpy
sprintf
strcat
strings
memcpy Copy data between tow memory locations.


Top Master Index Keywords Functions


Martin Leslie