CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    what means these warning?

    "deprecated conversion from string constant to 'char*' [-Wwrite-strings]"
    i have 1 struture:
    Code:
    struct SetText{
    	int PosX;
    	int PosY;
    	char *Text;
    
    
    };
    and heres how i add the values:
    Code:
    SetText *x=new SetText;	x->PosX=5;
    	x->PosX=6;
    	x->Text ="hello mother";
    why i recive that warning in: x->Text ="hello mother"; ?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: what means these warning?

    Well, you are not the first who got this error!
    https://www.google.de/search?q=depre...wwrite-strings
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: what means these warning?

    Quote Originally Posted by VictorN View Post
    Well, you are not the first who got this error!
    https://www.google.de/search?q=depre...wwrite-strings
    but i think ,more and i resolve it
    now the Text is string and what i did was recreate another var char pointer and then convert the values
    Code:
     char * writable = new char[p->Text.size() + 1];
    	std::copy(p->Text.begin(), p->Text.end(), writable);
    	int len = strlen(writable);
    and now no warnings
    thanks for all

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: what means these warning?

    Quote Originally Posted by Cambalinho View Post
    "deprecated conversion from string constant to 'char*' [-Wwrite-strings]"
    i have 1 struture:
    Code:
    struct SetText{
    	int PosX;
    	int PosY;
    	char *Text;
    
    
    };
    and heres how i add the values:
    Code:
    SetText *x=new SetText;	x->PosX=5;
    	x->PosX=6;
    	x->Text ="hello mother";
    why i recive that warning in: x->Text ="hello mother"; ?
    Which compiler are you using for this? You are probably receiving this warning because you are allocating a const char array ("hello mother") to a non-const char pointer (char *Text). To get rid of the warning, try making Text a const char *

    Code:
    struct SetText{
    	int PosX;
    	int PosY;
            const char *Text;
    };
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: what means these warning?

    Quote Originally Posted by 2kaud View Post
    Which compiler are you using for this? You are probably receiving this warning because you are allocating a const char array ("hello mother") to a non-const char pointer (char *Text). To get rid of the warning, try making Text a const char *

    Code:
    struct SetText{
        int PosX;
        int PosY;
            const char *Text;
    };
    thanks but i thot that the const was only for variables that we can't change their values
    thanks for all

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: what means these warning?

    Quote Originally Posted by Cambalinho View Post
    thanks but i thot that the const was only for variables that we can't change their values
    thanks for all
    Const is for variables for which you don't change their values. But you are passing a const string ("hello mother") that cannot be changed. Also, with your struct SetText it is very unlikely that you will be modifying Text so const char *Text is the better definition anyhow.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: what means these warning?

    Quote Originally Posted by 2kaud View Post
    Const is for variables for which you don't change their values. But you are passing a const string ("hello mother") that cannot be changed. Also, with your struct SetText it is very unlikely that you will be modifying Text so const char *Text is the better definition anyhow.
    thanks for all.. thanks to both
    sorry, but seems that i can't Rate you again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured