//// main.c// 内存使用//// Created by zhangxueming on 15/6/8.// Copyright (c) 2015年 zhangxueming. All rights reserved.//#include#include #include //内存操作函数//1.申请内存//2.判断内存是否申请成功//3.对申请到的内存初始化(可选)//4.使用内存//5.释放内存//6.将指向该内存的指针赋值为NULL//int main(int argc, const char * argv[]) {// char *pstr = (char *)malloc(100*sizeof(char));// if (!pstr) {// return -1;// }// memset(pstr, 0, 100);// scanf("%s", pstr);// printf("%s\n", pstr);// free(pstr);// pstr =NULL;// return 0;//}//内存清空函数//void *memset(void *mem, int c, size_t n);//int main(int argc,const char *argv[])//{// //printf("size = %ld\n", sizeof(void));// //// int a[10]={1,2,3,4,5};//// memset(a, 0, 40);//// for (int i=0; i<10; i++) {//// printf("%d", a[i]);//// }// // char str[100]="hello world";// memset(str, 0, 100);// for (int i=0; i<100; i++) {// printf("%d", str[i]);// }// return 0;//}////void *calloc(size_t count, size_t size);//申请的内存空间大小 = count * size;//利用malloc memset 实现callocvoid *mycalloc(size_t count, size_t size){ void *p= malloc(count*size); if (!p) { return NULL; } memset(p, 0, count*size); return p;}//int main(int argc,const char *argv[])//{// int *pscore =mycalloc(10, sizeof(int));// for(int i=0; i<10; i++)// {// //scanf("%d", &pscore[i]);// scanf("%d", pscore+i);// }// for (int i=0; i<10; i++) {// printf("%d ", *(pscore+i));// }// free(pscore);// pscore = NULL ;// return 0;//}//内存查找函数//memchr//void *memchr(const void *dst, int c, size_t n);//int main(int argc,const char *argv[])//{// char str[100]="hello world";// // printf("%s\n", memchr(str, 'o', 100));//// return 0;//}//修改内存大小//realloc//void *realloc(void *, size_t);//int main(int argc,const char *argv[])//{// char *pstr = (char *)malloc(50*sizeof(char));//// printf("%p\n", pstr);// // scanf("%s", pstr);// // pstr = realloc(pstr, 1000*sizeof(char));// // printf("%p\n", pstr);// // printf("%s\n", pstr);//// return 0;//}//int main(int argc, const char *argv[])//{// char *pstr=malloc(100*sizeof(char));// if (!pstr) {// return -1;// }// scanf("%s", pstr);// printf("%s\n", pstr);// // realloc(pstr, 0);//<==> free(pstr)// //free(pstr);//同一块内存不能被释放两次// pstr = NULL;// // // return 0;//}//int main(int argc,const char *argv[])//{// char *pstr = realloc(NULL, 100);//<==> malloc(100)// if (!pstr) {// return -1;// }// scanf("%s", pstr);// printf("%s\n", pstr);// realloc(pstr, 0);// pstr = NULL;// return 0;//}#include //内存比较函数//int memcmp(const void *, const void *, size_t);//int main(int argc,const char *argv[])//{// char str1[20]="helloworld";// char str2[20]="helloaorld";// //int a[5]={1,3,5};// // int ret = memcmp(str1, str2, 20);// printf("ret = %d\n", ret);//// return 0;//}//内存拷贝函数//void *memcpy(void *dst, const void *src, size_t n);//dst 与src 指针指向的内存空间不能有重叠的部分//int main(int argc,const char *argv[])//{// char str1[100]="hello world";// char str2[100]={};// // str1[19]='A';// // memcpy(str2, str1, 20);// printf("str2 = %s\n", str2);// printf("str2[19]=%c\n", str2[19]);// return 0;//}//内存移动函数//void *memmove(void *dst, const void *src, size_t n);//dst 与src 指向的内存空间可以重叠//int main(int argc,const char *argv[])//{// char str[100]="hello world";// // memmove(str, &str[1], 11);// printf("str = %s\n", str);// // return 0;//}//strstrchar *mystrstr(const char * src, const char *substr){ while (*src) { char *tempstr = (char *)src; char *tempsubstr = (char *)substr; while (*tempstr==*tempsubstr) { tempsubstr++; tempstr++; if (*tempsubstr=='\0') { return (char *)src; } } src++; } return NULL;}//int main(int argc,const char *argv[])//{// char *str1 = "hello worworld qianfeng";// printf("%s\n", mystrstr(str1, "world"));// return 0;//}//memmem//void *memmem(const void *mem, size_t size1, const void *submem, size_t size2)//int main(int argc, const char *argv[])//{// char *str="helloworldqianfeng";// // printf("%s\n",memmem(str, 18, "world", 5));// return 0;//}//把数字字符串转换成整型//atoi//atofint main(int argc, const char *argv[]){ printf("%d\n", atoi(" 123abc")); return 0;}