From: Stephen Satchell <list@fluent2.pyramid.net>
To: Mohammed Khalid Ansari <khalid@ncst.ernet.in>,
Glynn Clements <glynn.clements@virgin.net>
Cc: linux c programming mailing list <linux-c-programming@vger.kernel.org>
Subject: Re: strange behavious
Date: Tue, 24 Dec 2002 02:12:32 -0800 [thread overview]
Message-ID: <5.2.0.9.0.20021224020627.01e09340@fluent2.pyramid.net> (raw)
In-Reply-To: <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
At 10:50 AM 12/24/02 +0530, Mohammed Khalid Ansari wrote:
> > > #include <stdio.h>
> > >
> > > void parse (char **);
> > > char *words[] = {"mohammed", "khalid", "ansari"};
> >
> > While the array of pointers will be mutable, the strings themselves
> > aren't; if you attempt to modify them, you will get a segmentation
> > fault.
> >
>
>Is this in ANSI C? Will you please elaborate why is it so with this array
>only and how it is different from the example you have given below because
>ultimately both examples are same ie array of pointers.
>
>As far as I know if there is an string in a code with sufficient memory,
>it can be overwritten unless it is static.
Look at the type for string constants. It is "const array of char". And,
based on the number of compilers that assign this type to a string
constant, it is ANSI C. (It was true for the original ANSI specification,
I don't know about the latest ones.)
This is at variance with K&R C, and has been documented.
Because the string constant has the "const" property, the compiler is free
to include it in a read-only space. In modern computers with worthwhile
operating systems, the hardware will then enforce the "const"-ness of the
string.
This is a good thing, because it avoids some of the more astonishing
results that happens when you change a string constant. Older Unix
compilers would store separate instances of each constant string; modern
compilers will re-use the constant for multiple instances.
The GCC compiler has switches to modify the behavior such that the old
Unix-style mechanisms can be used. RTFM.
Or learn how to deal with the new rules -- which is what I had to do, and
have found my programming life made a little easier by them. They make sense.
Satch
next prev parent reply other threads:[~2002-12-24 10:12 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-21 5:25 strange behavious Mohammed Khalid Ansari
2002-12-23 8:19 ` Glynn Clements
2002-12-24 5:20 ` Mohammed Khalid Ansari
2002-12-25 9:50 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
2002-12-24 10:12 ` Stephen Satchell [this message]
2002-12-25 12:28 ` Mohammed Khalid Ansari
2002-12-25 13:58 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
2002-12-25 21:39 ` Stephen Satchell
-- strict thread matches above, loose matches on Subject: below --
2002-12-23 8:27 Alvarez Alberto-AALVARB1
2002-12-24 5:15 ` Mohammed Khalid Ansari
2002-12-25 9:55 ` Glynn Clements
2002-12-25 12:31 ` Mohammed Khalid Ansari
2002-12-25 13:44 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5.2.0.9.0.20021224020627.01e09340@fluent2.pyramid.net \
--to=list@fluent2.pyramid.net \
--cc=glynn.clements@virgin.net \
--cc=khalid@ncst.ernet.in \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).