|
以下的指令說明,是以MSDN上的說明為主
並其適用於VC++的編譯器
若有不適用於其它C編譯器的地方
還請其它的高手
代為修改一下
另外小弟的目的在於能讓新手了解指令的說明及方法
以減少此類問題的發問
但小弟一個人的力量
畢竟有限
所以希望其它高手
能在有空的時候
幫忙整理其它常用的指令
字串複製指令 strcpy()、strncpy()
(1)strcpy()
1.指令說明:
原型為:
char *strcpy(char *strDestination, const char *strSource);
參數說明:
strDestination: 字串複製後要存放的位置(目的地的指標)
strSource: 字串複製的來源位置(來源的指標)
輸出:
執行完,後會將strDestination的位置作輸出
2.使用範例:
ex.
void main()
{
char szDestination[128] = "ABCDEF";
char szSource[] = "123456";
printf("執行Strcpy()指令前\n");
printf("szDestination=%s\n", szDestination);
strcpy(szDestination, szSource);
printf("執行Strcpy()指令後\n");
printf("szDestination=%s\n", szDestination);
system("pause");
}
輸出:
執行Strcpy()指令前
szDestination=ABCDEF
執行Strcpy()指令後
szDestination=123456
3.實作:
// 實作strcpy指令
// strDestination ==> 目的字串指標
// strSource ==> 來源字串指標
char* MyStrcpy(char *strDestination, const char *strSource)
{
char* p = NULL;
int nCount = 0;
// 這一個地方,是防止strDestination或strSource是指向NULL,所作的錯誤偵測
// 在strcpy() 並沒有這個錯誤偵測的功能
// 因此strcpy()中,傳入NULL參數,會使你的程式掛掉哦
if(strDestination == NULL || strSource == NULL)
{
return NULL;
}
// 將目的字串指標,放到p
p = strDestination;
// 若來源字串指標未讀到字串結尾'\0'
// 則繼續複製
while( *(strSource+nCount) != '\0')
{
// 將目前strSource + nCount所指的字元,放到p中(目的字串指標)
*p = *(strSource + nCount);
// 指向下一個位址(目的字串指標)
p++;
// 指向下一個位址(來源字串指標)
nCount++;
}
// 在目的字串指標加上一個字串結尾
p = '\0'
// 回傳目的字串指標
return strDestination;
}
ps:
strcpy使用上雖然方便,但是若strSource(來源字串)的字元數,比strDestination(目的字串)
的陣列所能存放的字元還多時,則會發生陣列爆掉的問題(Buffer Overow);輕者程式掛掉
,重者系統安全會出現漏洞,因此建議將strcpy改用strncpy來代替,會比較好
MSDN對strcpy指令的說明
(2)strncpy()
1.指令說明:
原型為:
char *strncpy(char *strDest, const char *strSource, size_t count);
參數說明:
strDest: 字串複製後要存放的位置(目的地的指標)
strSource: 字串複製的來源位置(來源的指標)
count:最多從strSource複製多少個字元到strDest中
輸出:
執行完,後會將strDest的位置作輸出
2.使用範例:
ex.
void main()
{
// 強列建議在宣告szDestination前,要先作陣列初使化或在字串陣列的最後一個字元
// 放入'\0',防止szSource的字元數,比szDestination還大時
// 在執行完strncpy指令後,會因找不到字串結尾,而引起不必要的錯誤
char szDestination[5] = "ABCD";
char szSource[] = "12345689";
printf("執行strncpy()指令前\n");
printf("szDestination=%s\n", szDestination);
// 將szSource字串複製到szDestination中
// 複製的字元數不能超出szDestination的陣列大小
// sizeof(szDestination)-1為計算szDestination在預留字串結尾('\0')後
// 陣列所能存放的空間
strncpy(szDestination, szSource, sizeof(szDestination)-1);
printf("執行strncpy()指令後\n");
printf("szDestination=%s\n", szDestination);
system("pause");
}
輸出:
執行strncpy()指令前
szDestination=ABCD
執行strncpy()指令後
szDestination=1234
3.實作:
// 實作strncpy指令
// strDestination ==> 目的字串指標
// strSource ==> 來源字串指標
// count ==> 最多從strSource複製多少個字元到strDestination中
char* MyStrncpy(char *strDestination, const char *strSource, int count)
{
char* p = NULL;
int nCount = 0;
// 這一個地方,是防止strDestination或strSource是指向NULL,所作的錯誤偵測
// 在strncpy() 並沒有這個錯誤偵測的功能
// 因此strncpy()中,傳入NULL參數,會使你的程式掛掉哦
if(strDestination == NULL || strSource == NULL)
{
return NULL;
}
// 將目的字串指標,放到p
p = strDestination;
// 若來源字串指標未讀到字串結尾'\0'
// 而且所複製的字元數,未超過count所設的大小
// 則繼續複製
while( *(strSource+nCount) != '\0' && count > nCount)
{
// 將目前strSource + nCount所指的字元,放到p中(目的字串指標)
*p = *(strSource + nCount);
// 指向下一個位址(目的字串指標)
p++;
// 指向下一個位址(來源字串指標)
nCount++;
}
if(*(strSource+nCount) == '\0')
{
// 在目的字串指標加上一個字串結尾
p = '\0'
}
// 回傳目的字串指標
return strDestination;
}
MSDN對strncpy指令的說明
其它字串複製的指令,尚有
char *_strdup( const char *strSource );
等不常用的指令
|
|