C语言标准库函数总结

一.动态内存分配
1.malloc
  原型:extern void *malloc(unsigned int num_bytes);
  用法:#include <alloc.h>
  功能:分配长度为num_bytes字节的内存块
  说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
        当内存不再使用时,应使用free()函数将内存块释放。
  举例:
      // malloc.c
      #include <syslib.h>
      #include <alloc.h>

      main()
      {
        char *p;
        
        clrscr();        // clear screen

        p=(char *)malloc(100);
        if(p)
          printf("Memory Allocated at: %x",p);
        else
          printf("Not Enough Memory!\n");

        free(p);
        
        getchar();
        return 0;
      }

2.calloc
  原型:extern void *calloc(int num_elems, int elem_size);
  用法:#include <alloc.h>
  功能:为具有num_elems个长度为elem_size元素的数组分配内存
  说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
        当内存不再使用时,应使用free()函数将内存块释放。
  举例:
      // calloc.c
      #include <syslib.h>
      #include <alloc.h>

      main()
      {
        char *p;
        
        clrscr();        // clear screen

        p=(char *)calloc(100,sizeof(char));
        if(p)
          printf("Memory Allocated at: %x",p);
        else
          printf("Not Enough Memory!\n");
          
        free(p);

        getchar();
        return 0;
      }

3.realloc
  原型:extern void *realloc(void *mem_address, unsigned int newsize);
  用法:#include <alloc.h>
  功能:改变mem_address所指内存区域的大小为newsize长度。
  说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
        当内存不再使用时,应使用free()函数将内存块释放。
  举例:
      // realloc.c
      #include <syslib.h>
      #include <alloc.h>

      main()
      {
        char *p;
        
        clrscr();        // clear screen

        p=(char *)malloc(100);
        if(p)
          printf("Memory Allocated at: %x",p);
        else
          printf("Not Enough Memory!\n");
          
        getchar();

        p=(char *)realloc(p,256);
        if(p)
          printf("Memory Reallocated at: %x",p);
        else
          printf("Not Enough Memory!\n");

        free(p);
        
        getchar();
        return 0;
      }

4.free
  原型:extern void free(void *p);
  用法:#include <alloc.h>
  功能:释放指针p所指向的的内存空间。
  说明:p所指向的内存空间必须是用calloc,malloc,realloc所分配的内存。
        如果p为NULL或指向不存在的内存块则不做任何操作。
  举例:
      // free.c
      #include <syslib.h>
      #include <alloc.h>

      main()
      {
        char *p;
        
        clrscr();        // clear screen
        textmode(0x00);

        p=(char *)malloc(100);
        if(p)
          printf("Memory Allocated at: %x",p);
        else
          printf("Not Enough Memory!\n");
          
        getchar();
        free(p);         // release memory to reuse it

        p=(char *)calloc(100,1);
        if(p)
          printf("Memory Reallocated at: %x",p);
        else
          printf("Not Enough Memory!\n");

        free(p);         // release memory at program end
        
        getchar();
        return 0;
      }

二.字符串函数
1.memccpy
  原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count);
  用法:#include <string.h>
  功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。
  说明:返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL。ch被复制。
  举例:
      // memccpy.c
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Golden Global View";
        char d[20],*p;
        
        clrscr();
        
        p=memccpy(d,s,'x',strlen(s));
        if(p)
        {
          *p='\0';      // MUST Do This
          printf("Char found: %s.\n",d);
        }
        else
          printf("Char not found.\n");


        getchar();
        return 0;
      }

2.memchr
  原型:extern void *memchr(void *buf, char ch, unsigned count);
  用法:#include <string.h>
  功能:从buf所指内存区域的前count个字节查找字符ch。
  说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
  举例:
      // memchr.c
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Hello, Programmers!";
        char *p;
        
        clrscr();
        
        p=memchr(s,'P',strlen(s));
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");

        getchar();
        return 0;
      }

3.memcmp
  原型:extern int memcmp(void *buf1, void *buf2, unsigned int count);  
  用法:#include <string.h>
  功能:比较内存区域buf1和buf2的前count个字节。
  说明:
        当buf1<buf2时,返回值<0
        当buf1=buf2时,返回值=0
        当buf1>buf2时,返回值>0
  举例:
      // memcmp.c
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s1="Hello, Programmers!";
        char *s2="Hello, programmers!";
        int r;
        
        clrscr();
        
        r=memcmp(s1,s2,strlen(s1));
        if(!r)
          printf("s1 and s2 are identical");
        else
        if(r<0)
          printf("s1 less than s2");
        else
          printf("s1 greater than s2");
        

        getchar();
        return 0;
      }

4.memcpy
  原型:extern void *memcpy(void *dest, void *src, unsigned int count);
  用法:#include <string.h>
  功能:由src所指内存区域复制count个字节到dest所指内存区域。
  说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
  举例:
      // memcpy.c
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Golden Global View";
        char d[20];
        
        clrscr();
        
        memcpy(d,s,strlen(s));
        d[strlen(s)]=0;
        printf("%s",d);

        getchar();
        return 0;
      }

5.memicmp
  原型:extern int memicmp(void *buf1, void *buf2, unsigned int count);
  用法:#include <string.h>
  功能:比较内存区域buf1和buf2的前count个字节但不区分字母的大小写。
  说明:memicmp同memcmp的唯一区别是memicmp不区分大小写字母。
        当buf1<buf2时,返回值<0
        当buf1=buf2时,返回值=0
        当buf1>buf2时,返回值>0
  举例:
      // memicmp.c
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s1="Hello, Programmers!";
        char *s2="Hello, programmers!";
        int r;
        
        clrscr();
        
        r=memicmp(s1,s2,strlen(s1));
        if(!r)
          printf("s1 and s2 are identical");
        else
        if(r<0)
          printf("s1 less than s2");
        else
          printf("s1 greater than s2");
        

        getchar();
        return 0;
      }

posted @ 2018-09-06 19:26  稀客  阅读(445)  评论(0编辑  收藏  举报