#include "string.h"
main()
{char *pSrc = "hello,world";char *pDest =NULL; int iLen= strlen(pSrc);pDest = (char*)malloc(iLen);char *pD = pDest;char* pS = pSrc[iLen]; while(iLen--!=0){pD++ = pS--;}printf("%s", pDest);return 0;
}
在使用 malloc() 分配内存后,还需要使用 free() 函数释放内存,以避免内存泄漏问题
改正后的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main()
{char *pSrc = "hello,world";char *pDest = NULL;int iLen = strlen(pSrc);pDest = (char*)malloc(iLen + 1);char *pD = pDest;char *pS = pSrc + iLen - 1;while (iLen-- != 0){*pD++ = *pS--;}*pD = '\0';printf("%s", pDest);free(pDest);return 0;
}