代碼出處:新浪微博
今天在微博上發現了一個比較有意思的trim代碼,對於指針理解不深,可能看着會很繞!!在此和大家分享一下。。
這段代碼的關鍵之處就是3個指針操作同一塊內存。。
代碼如下
#define LEN 100
#define IS_SPACE(c)(c==' '||c=='\t')
void trim(char *str)
{
char *pcopy,*ptail;
if(!str)
return;
for(pcopy=str;*str&&IS_SPACE(*str);str++)
;
for(ptail=pcopy;*str;str++)
{
*pcopy++ =*str;
if(!IS_SPACE(*str))
ptail=pcopy;
*ptail=0;
}
}
注:trim函數實現的是輸入字符串前後去空格