469,904 Members | 1,956 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

char *s = "Google"

char *s = "Google"

Where this "Google" will be stored?
Why it can't be changed?
like s[1]='h'; -this is giving error
please explain this.

Dec 13 '06 #1
15 1609
Le 13-12-2006, Sri Ragaventhirar <sr***************@gmail.coma écrit*:
char *s = "Google"

Where this "Google" will be stored?
Somewhere you are not allowed to write in.
Why it can't be changed?
No.
like s[1]='h'; -this is giving error
please explain this.
You are trying to write in a forbiden place.
What you can do is:
char s[]= "Google";
and then, you are allowed to do
s[1]= 'h';

Marc Boyer
Dec 13 '06 #2
On 13 Dec 2006 00:55:28 -0800, "Sri Ragaventhirar"
<sr***************@gmail.comwrote:
>char *s = "Google"

Where this "Google" will be stored?
Why it can't be changed?
like s[1]='h'; -this is giving error
please explain this.
The answer is in FAQ 1.32.

http://c-faq.com/decl/strlitinit.html

--
jay
Dec 13 '06 #3
where this string "google"will be stored?
why it can't be changed?

Dec 13 '06 #4
where this string "google"will be stored?
why it can't be changed?

Dec 13 '06 #5
Le 13-12-2006, Sri Ragaventhirar <sr***************@gmail.coma écrit*:
where this string "google"will be stored?
It depends on your platform, ie which compiler, which options,
which OS...
why it can't be changed?
Because the standard states that...
Why the standard did this choice ? I assume this is to allow
this kind of string to be stored in read-only memory, which
could be cheaper in some platform...

Marc Boyer
Dec 13 '06 #6
thank you for your info
--------------------------------
Anywhere else, it turns into an unnamed, static array of characters,
and this unnamed array may be stored in read-only memory, and which
therefore cannot necessarily be modified. In an expression context, the
array is converted at once to a pointer, as usual (see section 6), so
the second declaration initializes p to point to the unnamed array's
first element.
-------------------------------
can you explain more about this read only memory(why it is like that
and where it is actually stored)(code segment data segment, stactk
segment???)

Dec 13 '06 #7
Sri Ragaventhirar wrote:
thank you for your info
--------------------------------
Anywhere else, it turns into an unnamed, static array of characters,
and this unnamed array may be stored in read-only memory, and which
therefore cannot necessarily be modified. In an expression context, the
array is converted at once to a pointer, as usual (see section 6), so
the second declaration initializes p to point to the unnamed array's
first element.
-------------------------------
can you explain more about this read only memory(why it is like that
and where it is actually stored)(code segment data segment, stactk
segment???)
It's like that because it is a string literal. It should require const
char*, but const is broken due to backwards compatibility in C.

Where its is stored is up to the implementation.

--
Ian Collins.
Dec 13 '06 #8
Sri Ragaventhirar wrote:

(several times. DON'T DO THAT. We are not a chatgroup.)
char *s = "Google"

Where this "Google" will be stored?
Somewhere that lasts from when the program starts to when it
ends. (Maybe even longer, but you can't tell.)
Why it can't be changed?
You don't know if it can or can't be. All you know is that
trying to change it gets undefined behaviour: in other words,
the implementation can do whatever it likes.
like s[1]='h'; -this is giving error
please explain this.
What kind of error? A compile-time error or a run-time one?

The compiler may choose to store the string in a region of
read-only memory. That memory may cause an signal to be raised
if you try and change it. BOOM.

The compiler may choose to store the string in a region of
read-only memory. That memory may ignore any attempt to change
it.

The compiler may choose to store the string in a region of
writeable memory. Assigning to elements of the string will
update it.

The compiler may choose to store the string in a region of
read-only memory. Writing to that memory may cause a
hedgehog to come and sign in your ear. Badly.

All four of these are legitimate implementations - even the
fourth, although the shortage of singing hedgehogs makes
it less likely you'll encounter it.

So don't assign into string literals.

--
Chris "We are BROKE-en" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Dec 13 '06 #9
Sri Ragaventhirar wrote:
thank you for your info
--------------------------------
Anywhere else, it turns into an unnamed, static array of characters,
and this unnamed array may be stored in read-only memory, and which
therefore cannot necessarily be modified. In an expression context, the
array is converted at once to a pointer, as usual (see section 6), so
the second declaration initializes p to point to the unnamed array's
first element.
-------------------------------
can you explain more about this read only memory(why it is like that
and where it is actually stored)(code segment data segment, stactk
segment???)
It's stored somewhere the implementation decides, wherever it's convenient.
Generally speaking one shouldn't need to care. If one /does/ care, one
consults one's local documentation or implementation-specific newsgroup.

Why do you care?

--
Chris "Perikles triumphant" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Dec 13 '06 #10
Sri Ragaventhirar wrote:
>
where this string "google"will be stored?
why it can't be changed?
You just got an answer, which you saw (as I can tell from your
headers) and didn't even bother to quote. So why are you posting
this? Are you trying to annoy and waste our time?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Dec 13 '06 #11
CBFalconer wrote:
Sri Ragaventhirar wrote:

where this string "google"will be stored?
why it can't be changed?

You just got an answer, which you saw (as I can tell from your
headers) and didn't even bother to quote. So why are you posting
this? Are you trying to annoy and waste our time?
Well, the answer given to his second question was "No.", which
is nonsensical. So re-posting the second question is reasonable.

Dec 14 '06 #12

Old Wolf wrote:
CBFalconer wrote:
Sri Ragaventhirar wrote:
>
where this string "google"will be stored?
why it can't be changed?
The character sequence is stored in a memory section which is readonly.
It can be anything. If you are cmpiling your program with highest level
of space optimization and the bit pattern of string "google" is
matching with some instruction machine code sequence, then compiler may
use that area of code segment as constant string and will give you
starting address of that in pointer s.

Dec 14 '06 #13
>The compiler may choose to store the string in a region of
>read-only memory. That memory may cause an signal to be raised
if you try and change it. BOOM.
SIGNUKE and SIGEMP are not mentioned in the C Standard, but when
either goes off, watch out!
>The compiler may choose to store the string in a region of
read-only memory. That memory may ignore any attempt to change
it.
Or, it could retaliate and shoot back.
>The compiler may choose to store the string in a region of
writeable memory. Assigning to elements of the string will
update it.

The compiler may choose to store the string in a region of
read-only memory. Writing to that memory may cause a
hedgehog to come and sign in your ear. Badly.
So hedgehogs don't know sign language very well? I suppose the
blame for that falls on the idiot who executed fflush(stdin).
>All four of these are legitimate implementations - even the
fourth, although the shortage of singing hedgehogs makes
it less likely you'll encounter it.
Don't you mean *SIGNING* hedgehogs? I suppose hedgehogs that can
both sing and translate their song into sign language are even
harder to find.
>So don't assign into string literals.
It causes Global Goredom, which causes Al Gore to talk less about
Global Cooling.
Dec 14 '06 #14
ma************@gmail.com writes:
Old Wolf wrote:
>CBFalconer wrote:
Sri Ragaventhirar wrote:

where this string "google"will be stored?
why it can't be changed?

The character sequence is stored in a memory section which is readonly.
[...]

Not necessarily. An implementation can legally store string literals
in read/write memory. Attempting to modify a string literal isn't
exactly forbidden; it invokes undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 14 '06 #15
Gordon Burditt wrote:

.... forgetting to attribute >as me ...
>>The compiler may choose to store the string in a region of
read-only memory. Writing to that memory may cause a
hedgehog to come and sign in your ear. Badly.

So hedgehogs don't know sign language very well? I suppose the
blame for that falls on the idiot who executed fflush(stdin).
Argh. I was so /sure/ I'd fixed the sign-for-sing typo. Probably
that was the later paragraph ... Reading is /such/ an
unreliabell processs.

--
Chris "signing in the rain" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Dec 14 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by junky_fellow | last post: by
4 posts views Thread by Stef Mientki | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.