Chinaunix首页 | 论坛 | 博客
  • 博客访问: 37083
  • 博文数量: 12
  • 博客积分: 560
  • 博客等级: 中士
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 15:26
文章分类

全部博文(12)

文章存档

2010年(12)

我的朋友

分类: C/C++

2010-07-27 16:04:55

(strcpy)和内存拷贝(memcpy)有什么不同?它们适合于在哪种情况下使用?

    strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串中,当遇到字符串末尾的null字符(\0)时,它会删去该字符,并结束拷贝。
    memcpy()
函数可以拷贝任意类型的数据。因为并不是所有的数据都以null字符结束,所以你要为memcpy()函数指定要拷贝的字节数。
     
在拷贝字符串时,通常都使用strcpy()函数;在拷贝其它数据(例如结构)时,通常都使用memcpy()函数。

例如:

#include
#include
typedef struct cust_str {
    int id ;
    char last_name [20] ;
    char first_name[15];
} CUSTREC;

int main (void)
{
    char * src_string = "This is the source string" ;
    char dest_string[50];
    CUSTREC src_cust;
    CUSTREC dest_cust;
    printf("Hello! I'm going to copy src_string into dest_string!\n");
    /* Copy src_ string into dest-string. Notice that the destination
      string is the first argument. Notice also that the strcpy()
      function returns a pointer to the destination string. */
    printf("Done! dest_string is: %s\n" ,
        strcpy(dest_string, src_string)) ;
    printf("Encore! Let's copy one CUSTREC to another. \n") ;
    prinft("I'll copy src_cust into dest_cust. \n");
    /* First, intialize the src_cust data members. */
    src_cust. id = 1 ;
    strcpy(src_cust. last_name, "Strahan");
    strcpy(src_cust. first_name, "Troy");
    /* Now, Use the memcpy() function to copy the src-cust structure to
        the dest_cust structure. Notice that, just as with strcpy(), the
        destination comes first. */
    memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
    printf("Done! I just copied customer number # %d (%s %s). " ,
        dest_cust. id, dest_cust. first_name, dest_cust. last_name) ;

   system("pause");

   return (0);
}

//删去字符串尾部空格的函数
/* The rtrim() function removes trailing spaces from a string. */

char * rtrim(char*str)

{

    int n = strlen(str) -1;/* Start at the character BEFORE
                               the null character (\0). */

    while(n>0)

    {

        if(*(str+n) != '')/* If we find a nonspace character: */

        {

              *(str+n+1) = '\0';/* Put the null character at one
                                     character past our current
                                     position. */

              break;

        }

        else

         {

              n--;

          }

    }

    return str;*Return a pointer to the string*/

}

测试:
int main(void)
{
    char trail_str[] = "This string has trailing spaces in it   ";
   /* Show the status of the string before calling the rtrim()
       function. */
    printf("Before calling rtrim(), trail_str is '%s'\n" , trail_str);
    printf("and has a length of %d. \n" , strlen (trail_str));
   /* Call the rtrimO function to remove the trailing blanks. */
    rtrim(trail_str) ;
   /* Show the status of the string
       after calling the rtrim() function. */
    printf("After calling rttim(), trail_ str is '%s'\n", trail_str );
    printf ("and has a length of %d. \n" , strlen(trail_str)) ;
   
    system("pause");
    return(0);
}

阅读(382) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~