伊莉討論區

標題: [已解決]新手詢問有關於strcpy的使用 [打印本頁]

作者: peterma    時間: 2018-3-24 09:07 PM     標題: [已解決]新手詢問有關於strcpy的使用

本帖最後由 peterma 於 2018-3-25 06:45 PM 編輯

#include<iostream>
#include<string.h>

using namespace std;
class  student{
private:
    char name[20];
public:
    student(){

    };
    student(char*s){
        strcpy(name,s);
            
    }
    void show_data(){
        cout << "姓名:"<< name <<endl;

        
    };
};
void main(){
    student s1;
    cout<<"------------------"<<endl;
    student s2("hello,world");
    s2.show_data();

    system("pause");
   

};

---------------

以上是我寫的程式碼,請問大大有人知道我到底哪裡錯了,沒辦法過一直顯示錯誤    1    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.    c:\users\petert\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\source.cpp    15    1    ConsoleApplication2






作者: z1090128    時間: 2018-4-28 11:52 AM

請問兩個變數的大小可能不一樣也會造成這種情形嗎?  看上面s好像沒有固定大小
作者: jackyo04    時間: 2018-6-20 04:29 PM

比較的變數長度要一樣喔,例如:strcpy(a,b),b的長度不能比a大,不然會造成內存地址溢出,然後就出現錯誤訊息




歡迎光臨 伊莉討論區 (http://www76484.eyny.com/) Powered by Discuz!