Thread: 'strcpy' was declared deprecated warning

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    'strcpy' was declared deprecated warning

    Hi,
    i was writing a blank console aplication in ms. visual c++ 2005 express edition

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void){
    	char *a="test";
    	char *b="east";
    	strcpy(a,b);
    	cout<<a<<endl;
    	cout<<b<<endl;
    
    	int y=0;
    	cin>>y;
    }
    the above was code.
    when i compile it,i get''' 'strcpy' was declared deprecated''' warnings. how do i get rid of them? some1 pls spot my error..thanks alot

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can disable this specific warning in the IDE settings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
        char *a="test";
    Wouldn't this particular line create a pointer to a read-only string, meaning you can't strcpy to it?

    Anyway, if you include <string>, why don't you use std::string? If you really need the C-style char arrays, the proper header would be <cstring> (but <string> usually includes it on its own).

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void){
    	string a("test");
    	string b("east");
    	a = b;
    	cout<<a<<endl;
    	cout<<b<<endl;
    
    	cin.get();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A better question is why you're using char arrays in a C++ program to begin with.

    You didn't even include the correct header file for strcpy.

    Plus, had you tried to run it, it would have crashed in the attempt to modify a string constant. Yet another reason to get used to using std::string for all your C++ string needs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    As has been said already, you should probably be using C++ strings and if you do use C style strings you can't copy into a constant string.

    However, the reason that warning comes up is because Microsoft considers strcpy and other "dangerous" functions to be deprecated. That function is not currently deprecated by the C++ standard. The warnings can be annoying, since Microsoft's solution is to use their non-portable but safer alternatives. Just search disable and the warning number for specific instructions, or even better switch to C++ strings instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM