目錄
- 一.strcpy_s 函數/strcpy 函數簡介
- 1.strcpy 函數文法
- 2.strcpy_s 函數文法
- 二.strcpy/strcpy_s 函數實戰
- 三.猜你喜歡
零基礎 C/C++ 學習路線推薦 : C/C++ 學習目錄 >> C 語言基礎入門
一.strcpy_s 函數/strcpy 函數簡介
1. strcpy
函數文法
strcpy
/*
*描述:此類函數是用于對字元串進行複制(拷貝)。
*
*參數:
* [in] strSource:需要拷貝的字元串
* [out] strDestination:拷貝完成之後的字元串
*
*傳回值:指向 strDestination 這個字元串的指針
*/
char* strcpy(char* strDestination, const char* strSource);
2. strcpy_s
strcpy_s
/*
*描述:此類函數是用于對字元串進行複制(拷貝)。
*
*參數:
* [out] strDestination:拷貝完成之後的字元串
* [in] numberOfElements: strDestination目标緩沖區長度
* [in] strSource:需要拷貝的字元串
*
*傳回值:傳回一個整數,0表示複制成功,傳回非0值代表複制不成功,不同的值表示不同的錯誤,具體内容可以查閱MSDN手冊
*/
errno_t strcpy_s(char *strDestination , size_t numberOfElements , const char *strSource);
C 語言在
string.h
中
strcpy
函數和
strcpy_
s 函數,都可以完成 char 字元串拷貝,注意:
strcpy
strcpy_s
函數在拷貝過程中,如果遇到
'\0'
結束符,那麼直接結束拷貝;
memcpy
函數 /
memcpy_s
函數拷貝過程中就算遇到
'\0'
結束符也不會結束;
2.如果使用
strcpy
函數都會提示
error:4996
,編譯器更加推薦使用
strcpy_s
函數,否則也可以參考:error C4996: ‘fopen’: This function or variable may be unsafe
error C4996: 'memcpy': This function or variable may be unsafe.
Consider using memcpy_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.
3.不管是 strcpy_s 函數或者 strcpy 函數 必須保證
dst
空間足夠大,能夠容納
src
,如果
dst
記憶體空間大小比
src
更小,會導緻溢出錯誤,引起程式崩潰!可以通過 sizeof 函數檢視記憶體記憶體大小,舉個例子:
50ml
的水杯能倒進
500ml
的水杯沒問題,
500ml
的水杯倒進
50ml
的水杯,會溢出很多水;
二.strcpy/strcpy_s 函數實戰
在 char 字元串中有作介紹,字元串預設都是
'\0'
結尾,
strcpy
函數或者
strcpy_s
函數在拷貝過程中,如果遇到
'\0'
結束符,那麼直接結束拷貝,看下面例子:
/******************************************************************************************/
//@Author:猿說程式設計
//@Blog(個人部落格位址): www.codersrc.com
//@File:C語言教程 - C語言 strcpy 和 strcpy_s 函數差別
//@Time:2021/06/03 08:00
//@Motto:不積跬步無以至千裡,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include<string.h>
#include "windows.h"
//error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#pragma warning( disable : 4996)
int main()
{
char src[1024] = { "C/C++教程-strcpy函數\0 - www.codersrc.com" };
char dst1[1024] = { 0 };
char dst2[1024] = { 0 };
printf("strcpy之前 dst1:%s\n", dst1);
strcpy(dst1, src);
printf("strcpy之後 dst1:%s\n", dst1);
printf("---------------------------------\n");
printf("strcpy_s之前 dst2:%s\n", dst2);
strcpy_s(dst2,sizeof(dst2)/sizeof(char),src);
printf("strcpy_s之後 dst2:%s\n", dst2);
system("pause");
return 0;
}
/*
輸出:
strcpy之前 dst1:
strcpy之後 dst1:C/C++教程-strcpy函數
---------------------------------
strcpy_s之前 dst2:
strcpy_s之後 dst2:C/C++教程-strcpy函數
請按任意鍵繼續. . .
*/
重上面的輸出結果可以看出:
strcpy
strcpy_s
函數在拷貝的時候,如果遇到
'\0'
,那麼拷貝直接結束,是以上面使用
strcpy
/
strcpy_s
函數拷貝的時候,
dst
字元串明顯少了一段字元
" - www.codersrc.com"
;
三.猜你喜歡
- 安裝 Visual Studio
- 安裝 Visual Studio 插件 Visual Assist
- Visual Studio 2008 解除安裝
- Visual Studio 2003/2015 解除安裝
- 設定 Visual Studio 字型/背景/行号
- C 語言格式控制符/占位符
- C 語言邏輯運算符
- C 語言三目運算符
- C 語言逗号表達式
- C 語言自加自減運算符(++i / i++)
- C 語言 for 循環
- C 語言 break 和 continue
- C 語言 while 循環
- C 語言 do while 和 while 循環
- C 語言 switch 語句
- C 語言 goto 語句
- C 語言 char 字元串
- C 語言 strlen 函數
- C 語言 sizeof 函數
- C 語言 sizeof 和 strlen 函數差別
- C 語言 strcpy 函數
- C 語言 strcpy_s 函數
- C 語言 strcpy 和 strcpy_s 函數差別