-
August 23rd, 2013, 01:38 PM
#1
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"; ?
-
August 23rd, 2013, 02:20 PM
#2
Re: what means these warning?
Victor Nijegorodov
-
August 23rd, 2013, 02:28 PM
#3
Re: what means these warning?
-
August 23rd, 2013, 03:16 PM
#4
Re: what means these warning?
Originally Posted by Cambalinho
"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)
-
August 23rd, 2013, 04:50 PM
#5
Re: what means these warning?
Originally Posted by 2kaud
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
-
August 24th, 2013, 05:51 AM
#6
Re: what means these warning?
Originally Posted by Cambalinho
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)
-
August 24th, 2013, 05:54 AM
#7
Re: what means these warning?
Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|