473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1784
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alastair | last post by:
Hello guys, I've been building a search facility for an intranet site I'm part of developing and we've been building a search engine using Index Server. It mostly works, however there have been...
87
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at...
5
by: junky_fellow | last post by:
Each time i submit some pattern to "google", it shows search took 0.XX seconds for exploring millions of web pages. When i look for efficient ways of searching a string, they always say compare...
2
by: Phin | last post by:
Hi, I would like to have the "google suggests" functionality on a web page like it is here: http://www.google.com/webhp?complete=1&hl=en I have downloaded the AJAX .NET wrapper...
4
by: Stef Mientki | last post by:
hello, In a program I want to download (updated) files from google code (not the svn section). I could find a python script to upload files, but not for downloading. Anyone has a hint or a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.