what means these warning?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: what means these warning?

  1. #1
    Join Date
    Apr 2009
    Posts
    605

    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
    Wallisellen (ZH), Switzerland
    Posts
    17,267

    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
    605

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2009
    Posts
    605

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2009
    Posts
    605

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center