前幾天遇到一個C語言初學者提到的一個問題,代碼我作了一些修改,以下:數組
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[10] = "I love C";
char *pStart = &str[0];
char *pEnd = &str[strlen(str) - 1];
char *temp = NULL;
while(pStart < pEnd)
{
*temp = *pStart;
*pStart = *pEnd;
*pEnd = *temp;
pStart++;
pEnd--;
}
printf("now the string is %s\n", str);
return 0;
}
代碼的本意很簡單,定義了一個字符數組,想經過兩個指針,一個是pStart指向數組第一個元素,一個指針是pEnd指向數組最後一個元素,來逆序這個字符數組。編譯時是能夠經過的,能夠在運行時老是報錯:markdown
這個錯誤的緣由說隱蔽也不隱蔽,說容易發現也不是那麼容易發現的,我想讀者應該知道錯誤在哪裏,能夠在留言裏指出錯誤所在。一眼沒有發現錯誤緣由的朋友,可不要急着看評論哦~~~spa