The Open Source Swiss Army Knife

/code/c/ISO_c_basics/
/code/c/ISO_c_basics/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /c   /ISO_c_basics 
Permalink: /var/sirfsup/code/c/ISO_c_basics/strncpy_kde.c
Title: add
article options : please login   |  raw source view  

/****************************************************************
 * 
 * Purpose: Program to demonstrate the 'strncpy' function.
 * Author:  M J Leslie
 * Date:    03-Feb-96
 * 
 ****************************************************************/

#include <string.h>	/* strcpy */

void SafeCopy(char *Dest, int DestSize, char *Source);

main()
{
    char Text1[20]="Tracy Sorrell";		/* string buffer	*/
    char Text2[10]="Martin";       		/* string buffer	*/

    printf (" Original string contents are: %s\n", Text2);

    SafeCopy(Text2, sizeof(Text2), Text1);
  
    printf (" New string contents are: %s\n", Text2);

    strcpy(Text2, "Alex");
  
    printf (" Final string contents are: %s\n", Text2);

}

/****************************************************************/

void SafeCopy(
    char     *Dest,                   /* Destination buffer. */
    int       DestSize,
    char     *Source)                 /* Source data. */
{
  
    /* ...	Copy 'Source' into 'Dest'.
     * ...        'Dest' is padded with NULLs if 'Source' is smaller.. */
  
    strncpy(Dest, Source, DestSize);
  
    /* ...	Safety net! Add the NULL just in case 'Source' is larger
     * ...	than 'Dest'.  */
  
    Dest[DestSize-1] = '\0';
}

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top