Still stuck...

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Still stuck...

Post 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.
"Programmers are tools for converting caffeine into code."
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Still stuck...

Post 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
Mark139
Member
Member
Posts: 39
Joined: Mon Jan 15, 2007 2:32 pm

Re: Still stuck...

Post by Mark139 »

try

Code: Select all

const char*
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: Still stuck...

Post 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.

Mark139 wrote:try

Code: Select all

const char*
How about I need a string that is changing, not const ?
"Programmers are tools for converting caffeine into code."
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: Still stuck...

Post 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.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Still stuck...

Post by yemista »

oh, I never thought warnings really mattered. The more you know...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Still stuck...

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Mark139
Member
Member
Posts: 39
Joined: Mon Jan 15, 2007 2:32 pm

Re: Still stuck...

Post 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
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: Still stuck...

Post 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 @$$ :|
"Programmers are tools for converting caffeine into code."
Post Reply