字符串倒序
#include "stdafx.h" #include <stdio.h> #include "t11.h" #include <string.h> #include <stdlib.h> //以下代码是把一个字符串倒序 int t11() { char* src = "1234567890"; int len = strlen(src); char* dest = (char*)malloc(len + 1);// + 1 ,字符串尾部空间,’\0’ memset(dest , 0, len + 1);// 字符串尾部,’\0’ char* d = dest; char* s = src + len - 1;//- 1, 指向最后一个字符 while(len-- != 0){ *d = *s; d++; s--; } printf("dest = %s\n", dest); free(dest);//释放空间,以免造成内存汇泄 dest = NULL;//防止产生野指针 return 0; } /* dest = 0987654321 Press any key to continue . . . */
malloc: 记得 1.free(dest);//释放空间,以免造成内存汇泄 2. dest = NULL;//防止产生野指针