This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Help wanted
- From: Jane Liang <JLiang at lgc dot com>
- To: "'Ben Davis'" <bnd25 at cam dot ac dot uk>, Jane Liang <JLiang at lgc dot com>, "'GCC-help'" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 17 Mar 2003 10:52:27 -0600
- Subject: RE: Help wanted
Hi, Ben:
It works!
Thanks!
Jane
-----Original Message-----
From: Ben Davis [mailto:bnd25 at cam dot ac dot uk]
Sent: Monday, March 17, 2003 10:54 AM
To: Jane Liang; 'GCC-help'
Subject: Re: Help wanted
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