关于::memset()和memcpy()的用法!一定给分.谢谢.请问他们是不是C 标准里\面才有呢.为什么我们国内写的C教程好像都没有说到这两个东西.
请高手.指点.两个涵数怎么用.细写一下.谢谢.一定给分的.:)

解决方案 »

  1.   

    char szTemp[255];memset(szTemp, '\0', sizeof(szTemp));
      

  2.   

    Example
    /* MEMCPY.C: Illustrate overlapping copy: memmove
     * handles it correctly; memcpy does not.
     */#include <memory.h>
    #include <string.h>
    #include <stdio.h>char string1[60] = "The quick brown dog jumps over the lazy fox";
    char string2[60] = "The quick brown fox jumps over the lazy dog";
    /*                           1         2         3         4         5
     *                  12345678901234567890123456789012345678901234567890
     */void main( void )
    {
       printf( "Function:\tmemcpy without overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 40 );
       printf( "Destination:\t%s\n", string1 + 16 );
       memcpy( string1 + 16, string1 + 40, 3 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );   /* Restore string1 to original contents */
       memcpy( string1 + 16, string2 + 40, 3 );   printf( "Function:\tmemmove with overlap\n" );
       printf( "Source:\t\t%s\n", string2 + 4 );
       printf( "Destination:\t%s\n", string2 + 10 );
       memmove( string2 + 10, string2 + 4, 40 );
       printf( "Result:\t\t%s\n", string2 );
       printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );   printf( "Function:\tmemcpy with overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 4 );
       printf( "Destination:\t%s\n", string1 + 10 );
       memcpy( string1 + 10, string1 + 4, 40 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
    }Output
    Function:   memcpy without overlap
    Source:      fox
    Destination:   dog jumps over the lazy fox
    Result:      The quick brown fox jumps over the lazy fox
    Length:      43 charactersFunction:   memmove with overlap
    Source:      quick brown fox jumps over the lazy dog
    Destination:   brown fox jumps over the lazy dog
    Result:      The quick quick brown fox jumps over the lazy dog
    Length:      49 charactersFunction:   memcpy with overlap
    Source:      quick brown dog jumps over the lazy fox
    Destination:   brown dog jumps over the lazy fox
    Result:      The quick quick brown dog jumps over the lazy fox
    Length:      49 characters
      

  3.   

    ////////////////memset//////////////////////////
    void *memset( void *dest, int c, size_t count );他是用来对一块内存进行初始化的函数,比如我声明一个数组char a[100];这时我要求先把这段内存所有的值设为零。则可以memset(a,0x00,100)!msdn中的文档:
    memset
    Sets buffers to a specified character.void *memset( void *dest, int c, size_t count );Routine Required Header Compatibility 
    memset <memory.h> or <string.h> ANSI, Win 95, Win NT 
    For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version 
    LIBCMT.LIB Multithread static library, retail version 
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version 
    Return Valuememset returns the value of dest.ParametersdestPointer to destinationcCharacter to setcountNumber of charactersResThe memset function sets the first count bytes of dest to the character c.Example/* MEMSET.C: This program uses memset to
     * set the first four bytes of buffer to "*".
     */#include <memory.h>
    #include <stdio.h>void main( void )
    {
       char buffer[] = "This is a test of the memset function";   printf( "Before: %s\n", buffer );
       memset( buffer, '*', 4 );
       printf( "After:  %s\n", buffer );
    }
    OutputBefore: This is a test of the memset function
    After:  **** is a test of the memset function
    Buffer Manipulation RoutinesSee Also   _memccpy, memchr, memcmp, memcpy, _strnset/////////memcpy
    memcpy 
    功  能: 从源source中拷贝n个字节到目标destin中 
    用  法: void *memcpy(void *destin, void *source, unsigned n); 
    memcpy是把source 指向的对象中的n个字符拷贝到destin所指向的对象中,返回指向结果对象的指针。程序例: #include <stdio.h> 
    #include <string.h> 
    int main(void) 

       char src[] = "******************************"; 
       char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; 
       char *ptr; 
       printf("destination before memcpy: %s\n", dest); 
       ptr = memcpy(dest, src, strlen(src)); 
       if (ptr) 
          printf("destination after memcpy:  %s\n", dest); 
       else 
          printf("memcpy failed\n"); 
       return 0; 
      

  4.   

    memcpy 
    原型:extern void *memcpy(void *dest, void *src, unsigned int count); 
    用法:#include <string.h> 
    功能:由src所指内存区域复制count个字节到dest所指内存区域。 
    说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。 
    memset 
    原型:extern void *memset(void *buffer, int c, int count); 
    用法:#include <string.h> 
    功能:把buffer所指内存区域的前count个字节设置成字符c。 
    说明:返回指向buffer的指针。 memset用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘\0’;例:char a[100];memset(a, '\0', sizeof(a)); memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。 
      

  5.   


    唉,又來晚了,樓上的老兄都說完了。
    還是說一下吧
    //===========================================================================void *memset( void *dest, int c, size_t count );
    Parameters
    dest 
       Pointer to destination 

       Character to set 
    count 
       Number of characters Example
    /* MEMSET.C: This program uses memset to
     * set the first four bytes of buffer to "*".
     */#include <memory.h>
    #include <stdio.h>void main( void )
    {
       char buffer[] = "This is a test of the memset function";   printf( "Before: %s\n", buffer );
       memset( buffer, '*', 4 );
       printf( "After:  %s\n", buffer );
    }Output
    Before: This is a test of the memset function
    After:  **** is a test of the memset function//===========================================================================
    void *memcpy( void *dest, const void *src, size_t count );
    Parameters
    dest 
    New buffer 
    src 
    Buffer to copy from 
    count 
    Number of characters to copy 
    #include <memory.h>
    #include <string.h>
    #include <stdio.h>char string1[60] = "The quick brown dog jumps over the lazy fox";
    char string2[60] = "The quick brown fox jumps over the lazy dog";
    /*                           1         2         3         4         5
     *                  12345678901234567890123456789012345678901234567890
     */void main( void )
    {
       printf( "Function:\tmemcpy without overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 40 );
       printf( "Destination:\t%s\n", string1 + 16 );
       memcpy( string1 + 16, string1 + 40, 3 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );   /* Restore string1 to original contents */
       memcpy( string1 + 16, string2 + 40, 3 );   printf( "Function:\tmemmove with overlap\n" );
       printf( "Source:\t\t%s\n", string2 + 4 );
       printf( "Destination:\t%s\n", string2 + 10 );
       memmove( string2 + 10, string2 + 4, 40 );
       printf( "Result:\t\t%s\n", string2 );
       printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );   printf( "Function:\tmemcpy with overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 4 );
       printf( "Destination:\t%s\n", string1 + 10 );
       memcpy( string1 + 10, string1 + 4, 40 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
    }Output
    Function:   memcpy without overlap
    Source:      fox
    Destination:   dog jumps over the lazy fox
    Result:      The quick brown fox jumps over the lazy fox
    Length:      43 charactersFunction:   memmove with overlap
    Source:      quick brown fox jumps over the lazy dog
    Destination:   brown fox jumps over the lazy dog
    Result:      The quick quick brown fox jumps over the lazy dog
    Length:      49 charactersFunction:   memcpy with overlap
    Source:      quick brown dog jumps over the lazy fox
    Destination:   brown dog jumps over the lazy fox
    Result:      The quick quick brown dog jumps over the lazy fox
    Length:      49 characters你要想全面了解這類函數,還要比較別的具有這樣功能的函數,比如_memccpy, memchr, memcmp, memmove, memset, strcpy, strncpy以上這幾個函數的功能你全部都了解嗎?
    如果還沒有,趁這個機會一並都學一下吧,對你有好處的