下载此文档

C语言库函数C库函数.ppt


文档分类:IT计算机 | 页数:约36页 举报非法文档有奖
1/36
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/36 下载此文档
文档列表 文档介绍
C语言常用库函数版本:讲师:联系方式:课程编号:()函数名:malloc 功能:内存分配函数 用法:void*malloc(unsignedsize); 程序例: #include<>/*For_MAX_PATHdefinition*/#include<> #include<> voidmain(void) { char*string;/*Allocatespaceforapathname*/ string=malloc(_MAX_PATH); if(string==NULL) printf("Insufficientmemoryavailable\n"); else{ printf("Memoryspaceallocatedforpathname\n"); free(string); printf("Memoryfreed\n"); } }动态存储分配()函数名:free 功能:释放已分配的块 用法:voidfree(void*ptr); 程序例: #include<> #include<> #include<> intmain(void) { char*str; /*allocatememoryforstring*/ str=malloc(10); /*copy"Hello"tostring*/ strcpy(str,"Hello"); /*displaystring*/ printf("Stringis%s\n",str); /*freememory*/ free(str); return0; }动态存储分配()函数名:realloc 功能:改变已分配内存的大小,ptr为已分配有内存区域的指针,newsize为新的长度,返回分配好的内存指针; 用法:void*realloc(void*ptr,unsignednewsize) 程序例: #include<> #include<> #include<> voidmain(void) { long*buffer; size_tsize; if((buffer=(long*)malloc(1000*sizeof(long)))==NULL) exit(1); size=_msize(buffer); printf("Sizeofblockaftermallocof1000longs:%u\n",size); /*Reallocateandshownewsize:*/ if((buffer=realloc(buffer,size+(1000*sizeof(long))))==NULL) exit(1); size=_msize(buffer); printf("Sizeofblockafterreallocof1000morelongs:%u\n",size); free(buffer); exit(0); }动态存储分配()函数名:calloc 功能:分配nelem个长度为elsize的内存空间并返回所分配内存的指针; 用法:void*calloc(size_tnum,size_tsize); 程序例: #include<> #include<> voidmain(void) { long*buffer; buffer=(long*)calloc(40,sizeof(long)); if(buffer!=NULL) printf("Allocated40longintegers\n"); else printf("Can'tallocatememory\n"); free(buffer); }类型转换函数()函数名:atof 功能:把字符串转换成双精度数,并返回这个数,错误返回0; 用法:doubleatof(constchar*nptr); 程序例: #include<> #include<> intmain(void) { floatf; char*str=""; f=atof(str); printf("string=%sfloat=%f\n",str,f); return0; }类型转换函数()函数名:atoi 功能:把字符串转换成整型数,并返回这个数,错误返回0; 用法:intat

C语言库函数C库函数 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数36
  • 收藏数0 收藏
  • 顶次数0
  • 上传人ouyangxiahe
  • 文件大小297 KB
  • 时间2019-05-22