欢迎来到天天文库
浏览记录
ID:19468518
大小:310.00 KB
页数:32页
时间:2018-10-02
《理学c库函数ppt模版课件》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言回顾课程概述math.hstdio.hstdlib.hmalloc.hstring.hassert.h动态存储分配(malloc.h)函数名:malloc功能:内存分配函数用法:void*malloc(unsignedsize);程序例:#include/*For_MAX_PATHdefinition*/#include#includevoidmain(void){char*string;/*Allocatespaceforapathname*/string=mall
2、oc(_MAX_PATH);if(string==NULL)printf("Insufficientmemoryavailable");else{printf("Memoryspaceallocatedforpathname");free(string);printf("Memoryfreed");}}动态存储分配(malloc.h)函数名:free功能:释放已分配的块用法:voidfree(void*ptr);程序例:#include#include#include3、.h>intmain(void){char*str;/*allocatememoryforstring*/str=malloc(10);/*copy"Hello"tostring*/strcpy(str,"Hello");/*displaystring*/printf("Stringis%s",str);/*freememory*/free(str);return0;}动态存储分配(malloc.h)函数名:realloc功能:改变已分配内存的大小,ptr为已分配有内存区域的指针,newsize为新的长度,返回分配好的内存指4、针;用法:void*realloc(void*ptr,unsignednewsize)程序例:#include#include#includevoidmain(void){long*buffer;size_tsize;if((buffer=(long*)malloc(1000*sizeof(long)))==NULL)exit(1);size=_msize(buffer);printf("Sizeofblockaftermallocof1000longs:%u",size5、);/*Reallocateandshownewsize:*/if((buffer=realloc(buffer,size+(1000*sizeof(long))))==NULL)exit(1);size=_msize(buffer);printf("Sizeofblockafterreallocof1000morelongs:%u",size);free(buffer);exit(0);}动态存储分配(malloc.h)函数名:calloc功能:分配nelem个长度为elsize的内存空间并返回所分配内存的指针;用法:void6、*calloc(size_tnum,size_tsize);程序例:#include#includevoidmain(void){long*buffer;buffer=(long*)calloc(40,sizeof(long));if(buffer!=NULL)printf("Allocated40longintegers");elseprintf("Can'tallocatememory");free(buffer);}类型转换函数(stdlib.h)函数名:atof功能:把字符串转换7、成双精度数,并返回这个数,错误返回0;用法:doubleatof(constchar*nptr);程序例:#include#includeintmain(void){floatf;char*str="12345.67";f=atof(str);printf("string=%sfloat=%f",str,f);return0;}类型转换函数(stdlib.h)函数名:atoi功能:把字符串转换成整型数,并返回这个数,错误返回0;用法:intatoi(constchar*nptr);程序例8、:#include#includeintmain(void){intn;char*str="12345.67";n=atoi(str);printf("string=%sinteger=%d
3、.h>intmain(void){char*str;/*allocatememoryforstring*/str=malloc(10);/*copy"Hello"tostring*/strcpy(str,"Hello");/*displaystring*/printf("Stringis%s",str);/*freememory*/free(str);return0;}动态存储分配(malloc.h)函数名:realloc功能:改变已分配内存的大小,ptr为已分配有内存区域的指针,newsize为新的长度,返回分配好的内存指
4、针;用法:void*realloc(void*ptr,unsignednewsize)程序例:#include#include#includevoidmain(void){long*buffer;size_tsize;if((buffer=(long*)malloc(1000*sizeof(long)))==NULL)exit(1);size=_msize(buffer);printf("Sizeofblockaftermallocof1000longs:%u",size
5、);/*Reallocateandshownewsize:*/if((buffer=realloc(buffer,size+(1000*sizeof(long))))==NULL)exit(1);size=_msize(buffer);printf("Sizeofblockafterreallocof1000morelongs:%u",size);free(buffer);exit(0);}动态存储分配(malloc.h)函数名:calloc功能:分配nelem个长度为elsize的内存空间并返回所分配内存的指针;用法:void
6、*calloc(size_tnum,size_tsize);程序例:#include#includevoidmain(void){long*buffer;buffer=(long*)calloc(40,sizeof(long));if(buffer!=NULL)printf("Allocated40longintegers");elseprintf("Can'tallocatememory");free(buffer);}类型转换函数(stdlib.h)函数名:atof功能:把字符串转换
7、成双精度数,并返回这个数,错误返回0;用法:doubleatof(constchar*nptr);程序例:#include#includeintmain(void){floatf;char*str="12345.67";f=atof(str);printf("string=%sfloat=%f",str,f);return0;}类型转换函数(stdlib.h)函数名:atoi功能:把字符串转换成整型数,并返回这个数,错误返回0;用法:intatoi(constchar*nptr);程序例
8、:#include#includeintmain(void){intn;char*str="12345.67";n=atoi(str);printf("string=%sinteger=%d
此文档下载收益归作者所有