This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Help wanted
- From: Ben Davis <bnd25 at cam dot ac dot uk>
- To: Jane Liang <JLiang at lgc dot com>,"'GCC-help'" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 17 Mar 2003 16:54:29 +0000
- Subject: Re: Help wanted
- Organization: University of Cambridge
- References: <57A168CAC1B2A6409978DF309F6FDB39C4EB12@lgchexch008.ad.lgc.com>
On Monday 17 March 2003 4:26 pm, Jane Liang wrote:
> Line 8 char *str = "0.12000, 0.0, 0.0";
This is a constant string, so it is stored in read-only memory.
> Line 13 *ptr = '\0';
You are now writing to read-only memory. Hence the crash.
You can fix your code by making 'str' an array instead:
char str[] = "0.12000, 0.0, 0.0";
Then the string will be stored temporarily on the stack, which is writable.
The array will be just the right size to accommodate the string.
When using GCC, I recommend you compile with -Wwrite-strings. String constants
will then be given the 'const' qualifier, and you will get a warning if you
don't use 'const' yourself where necessary:
const char *str = "0.12000, 0.0, 0.0";
Ben