一.动态内存分配
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;
      }

C语言标准库函数总结的更多相关文章

  1. C语言标准库函数(网络上copy的)

    C语言标准库函数 标准io函数Standard C I/Oclearerr() clears errorsfclose() close a filefeof() true if at the end- ...

  2. C语言标准库函数strcpy与strcmp的简单实现

    //C语言标准库函数strcpy的一种简单实现. //返回值:目标串的地址. //对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL. //参数:des为目标字符串, ...

  3. C语言标准库函数memcpy和memmove的区别以及内存重叠问题处理

    ①memcpy()和memmove()都是C语言中的标准库函数,定义在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, cons ...

  4. C语言标准库函数qsort具体解释

    1 函数简单介绍 功 能: 使用高速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(c ...

  5. 文件I/O之C标准库函数和系统库函数差别

    1.首先C标准库函数是工作在系统库函数之上的.C标准库函数在读写文件时候都有一个文件流指针.FILE*fp=NULL;// fp=fopen(F_PATH,"r"); fp文件流指 ...

  6. strcpy c标准库函数

    C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. 已知strcpy函数的原型是: char *strcpy(char *dst, const ...

  7. C语言库函数,头文件

    参看:https://zhidao.baidu.com/question/328173842.html 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五种类型.一些宏和通用工具函数. ...

  8. C++ 中库函数bsearch的简单研究(含示例)

    /**//*bsearch函数声明如下: void *bsearch(const void *key, const void *base, size_t *nelem,                 ...

  9. 关于fefo函数

    feof是C语言标准库函数函数,其原型在stdio.h中,其功能是检测流上的文件结束符. 函数原型: int feof(FILE *stream); 返回值:如果文件结束,则返回非0值,否则返回0 在 ...

随机推荐

  1. win7/8系统中php5.3和5.4、5.5不能加载php_curl.dll解决办法

    win7/8系统中php5.3和5.4.5.5不能加载php_curl.dll解决办法   作者:用户 来源:互联网 时间:2016-06-23 18:54:33 php变量注释系统模块 摘要: 本文 ...

  2. icons使用

    1.将选中图标加入项目 2.unicode方式查看连接在线连接 3.复制代码到样式表 4.引用样式,并设置I标签,颜色和大小可以通过设置i标签color和font-size进行调整 <i cla ...

  3. 《windows核心编程系列》十九谈谈使用远程线程来注入DLL。

    windows内的各个进程有各自的地址空间.它们相互独立互不干扰保证了系统的安全性.但是windows也为调试器或是其他工具设计了一些函数,这些函数可以让一个进程对另一个进程进行操作.虽然他们是为调试 ...

  4. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  5. 51nod 1116 K进制下的大数

    你万万想不到,Long Long 就能存下的数据 #include <iostream> #include <cstdio> #include <cstdlib> ...

  6. BP神经网络算法改进

    周志华机器学习BP改进 试设计一个算法,能通过动态调整学习率显著提升收敛速度,编程实现该算法,并选择两个UCI数据集与标准的BP算法进行实验比较. 1.方法设计 传统的BP算法改进主要有两类: - 启 ...

  7. 暴力/图论 hihoCoder 1179 永恒游戏

    题目传送门 /* 暴力:也是暴力过了,无语.无向图,两端点都要加度数和点 */ #include <cstdio> #include <algorithm> #include ...

  8. linux下的c程序排版工具:indent 分类: linux 2014-06-14 20:05 720人阅读 评论(0) 收藏

    Linux下有一个方便的c语言程序排版工具,只要选择恰当的参数,可以轻易地使自己的程序具有统一的风格. 当然首先要安装indent,执行命令:apt-get install indent indent ...

  9. canvas 保存状态

    1.保存和恢复绘图状态: 在绘制图形时,难免会重复使用某个样式,甚至有时会在不同颜色之间来回切换. 那么为了减少代码冗余,我们可以调用画布中的save()方法,来帮我们 保存一些样式和属性,这样我们就 ...

  10. CSS综合用法

    div 居中 {position: absolute; top: 50%; left: 50%; margin-top: -180px; margin-left: -160px;}