Page 1 of 1
Still stuck...
Posted: Fri May 08, 2009 8:59 am
by quanganht
So, I got GCC cross-compiler set up (never mind the version, still the same error).
Eg: I have
Code: Select all
char* str = (char*)"HELLO WORLD!";
then GCC says
Code: Select all
error: initialization discards qualifiers from pointer target type
How can I get rid of it ??? (Make it totally disappear)
Note: I have options -Werror. Remove this just don't help, as warnings pop up instead.
Re: Still stuck...
Posted: Fri May 08, 2009 9:02 am
by yemista
Will the code actually work? -Werror just means treat warning as errors, so removing it would give you a warning instead, but just because the compiler is giving you a warning doesnt mean the code wont do what you want it to. A warning is just a warning, it is not an error
Re: Still stuck...
Posted: Fri May 08, 2009 9:11 am
by Mark139
Re: Still stuck...
Posted: Fri May 08, 2009 9:12 am
by quanganht
Of course it work, if:
- I make : char* str =
(char*)"HELLO WORLD!" instead. See,
(char*). And everytime I want to write a string,
(char*) again.
- Or delete -Werror and pretend not to care about it.
Everyone know warnings is warnings, but I hate them. Because warnings can suddenly turn into errors and nobody can find that goddamn bug.
How about I need a string that is changing, not const ?
Re: Still stuck...
Posted: Fri May 08, 2009 9:44 am
by skyking
Read the manual for GCC, it looks like you have somehowe enabled:
* Warnings are errors (as stated).
* Make string literals const char[] (-Wwrite-strings).
* Warn for casting away cv-qualifiers (-Wcast-qual).
Basically modifying a string literal has undefined/implementation-defined behavior. IFAIK with GCC you'll have some problems here. The string literal ends up in the .rodata section which is for read-only data. You could of course map the read-only data to read-write pages and you should be able to modify the literal, but you'd be able to modify all constant data as well. To avoid this you could put all modifiable string literals in one file and use link scripts to put the .rodata section from these in a read-write data section in the output. However in most cases you don't need/want to modify the string literals.
Re: Still stuck...
Posted: Fri May 08, 2009 9:49 am
by yemista
oh, I never thought warnings really mattered. The more you know...
Re: Still stuck...
Posted: Fri May 08, 2009 12:30 pm
by Combuster
And hiding warnings is a Bad Idea(tm), fix them. That's what "warnings as errors" (-Werror) is for - to force you to stick to good coding practices.
Re: Still stuck...
Posted: Fri May 08, 2009 1:12 pm
by Mark139
I don't like to write to literal strings. You are better off copying the string literal to a stack or heap buffer. I think the code is easier to read
Code: Select all
const char * msg = "Hello";
void foo()
{
char buffer[16];
strcpy(buffer,msg); // assuming you have strcpy :)
}
I prefer to see constants all in one place and I guess the compiler does too
Just my 2p worth
Re: Still stuck...
Posted: Fri May 08, 2009 5:42 pm
by quanganht
Combuster wrote:And hiding warnings is a Bad Idea(tm), fix them. That's what "warnings as errors" (-Werror) is for - to force you to stick to good coding practices.
That's why I keep sticking with -Werror, although it keep kicking my @$$