文档介绍: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