'strcpy' was declared deprecated warning

This is a discussion on 'strcpy' was declared deprecated warning within the C++ Programming forums, part of the General Programming Boards category; Hi, i was writing a blank console aplication in ms. visual c++ 2005 express edition Code: #include <iostream> #include <string> ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You can disable this specific warning in the IDE settings
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21