Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what's the difference between the next 2 declarations?

Reply
Thread Tools

what's the difference between the next 2 declarations?

 
 
Wade Yin
Guest
Posts: n/a
 
      04-05-2004
Hi,

What's the difference between:

char *x="abcde";

and

char y[]={"abcde"};


I can't modify the value of the variable x in functions, but y can be do.

Is there any other similar problem need to be notice?



Thanks in advance!

Wade Yin


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      04-05-2004
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <(E-Mail Removed)>
wrote in comp.lang.c:

> Hi,
>
> What's the difference between:
>
> char *x="abcde";


x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.

> and
>
> char y[]={"abcde"};


y is an array of 6 characters, initialized with the string literal.
The characters in y can be changed.

> I can't modify the value of the variable x in functions, but y can be do.
>
> Is there any other similar problem need to be notice?


No, the situation with string literals is a special case and there are
no others like it, except for wide string literals.

> Thanks in advance!
>
> Wade Yin


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Wade Yin
Guest
Posts: n/a
 
      04-05-2004

"Jack Klein" <(E-Mail Removed)>
??????:(E-Mail Removed). ..
> On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <(E-Mail Removed)>
> wrote in comp.lang.c:
>
> > Hi,
> >
> > What's the difference between:
> >
> > char *x="abcde";

>
> x is a pointer to the a six character (including the '\0') string
> literal. Attempting to modify a string literal causes undefined
> behavior. x itself can be modified to point to a different array of
> characters.


How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?


>
> > and
> >
> > char y[]={"abcde"};

>
> y is an array of 6 characters, initialized with the string literal.
> The characters in y can be changed.
>
> > I can't modify the value of the variable x in functions, but y can be

do.
> >
> > Is there any other similar problem need to be notice?

>
> No, the situation with string literals is a special case and there are
> no others like it, except for wide string literals.
>
> > Thanks in advance!
> >
> > Wade Yin

>
> --
> Jack Klein
> Home: http://JK-Technology.Com
> FAQs for
> comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
> comp.lang.c++ http://www.parashift.com/c++-faq-lite/
> alt.comp.lang.learn.c-c++
> http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



 
Reply With Quote
 
John Tsiombikas (Nuclear / the Lab)
Guest
Posts: n/a
 
      04-05-2004
Wade Yin wrote:
>>> char *x="abcde";

>>
>>x is a pointer to the a six character (including the '\0') string
>>literal. Attempting to modify a string literal causes undefined
>>behavior. x itself can be modified to point to a different array of
>>characters.

>
>
> How does "abcde" stored in memory? we can find x in memory, but we can't
> operate it?


Can't "change it", yes, the standard says that you cannot change a
string literal. The implementation has the freedom to place it in a
write-only protected memory segment, in ROM, or in fact anywhere it
desires. It may even put it exactly where it puts a modifiable character
array.

--
John Tsiombikas (Nuclear / the Lab)
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://thelab.demoscene.gr/nuclear/
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-05-2004
Wade Yin <(E-Mail Removed)> spoke thus:

> How does "abcde" stored in memory?


Exactly how is (I believe) up to the implementation, but commonly it
ends up in read-only memory.

> we can find x in memory, but we can't operate it?


Not without bad things potentially happening - depending on the
implementation, attempting to access memory denoted as read-only can
cause your program to crash.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      04-05-2004
On Mon, 5 Apr 2004 13:36:27 +0800, "Wade Yin" <(E-Mail Removed)> wrote:

>
>"Jack Klein" <(E-Mail Removed)>
>??????:(E-Mail Removed) ...
>> On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <(E-Mail Removed)>
>> wrote in comp.lang.c:
>>
>> > Hi,
>> >
>> > What's the difference between:
>> >
>> > char *x="abcde";

>>
>> x is a pointer to the a six character (including the '\0') string
>> literal. Attempting to modify a string literal causes undefined
>> behavior. x itself can be modified to point to a different array of
>> characters.

>
> How does "abcde" stored in memory? we can find x in memory, but we can't
>operate it?
>

If you go to this page:
http://www.bdsoft.com/courses/cprog-details.html
and click on the "slides" link, slides #33-42 illustrate the different ways
in which your two constructs are stored in memory, and the sorts of things
you're permitted to do with them.

(Note to rest of group: I haven't updated this material since I began
hanging out in clc, so there are probably lots of nits to pick WRT saying
what's const and what's not, etc. Rest assured I'll go over the entire
/course/ before I ever actually teach it again....)
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-05-2004
In <c4qpfg$7cm$(E-Mail Removed)99.com> "Wade Yin" <(E-Mail Removed)> writes:

> What's the difference between:
>
> char *x="abcde";
>
> and
>
> char y[]={"abcde"};


How about reading the FAQ *before* posting?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-05-2004
In <c4rlhk$8gh$(E-Mail Removed)> Christopher Benson-Manica <(E-Mail Removed)> writes:

>Wade Yin <(E-Mail Removed)> spoke thus:
>
>> How does "abcde" stored in memory?

>
>Exactly how is (I believe) up to the implementation, but commonly it
>ends up in read-only memory.


It's less common than you might think. Read-only string literals are a
C89 invention and many implementors have chosen compatibility with
pre-ANSI C programs. In pre-ANSI C, string literals were writable and
the compiler was not free to merge multiple identical string literals.
Some compilers (e.g. gcc) even let the user choose between the two
flavours of string literals:

-fwritable-strings
Store string constants in the writable data segment and don't
uniquize them. This is for compatibility with old programs
which assume they can write into string constants.

>> we can find x in memory, but we can't operate it?

>
>Not without bad things potentially happening - depending on the
>implementation, attempting to access memory denoted as read-only can
>cause your program to crash.


Well I can't find anything wrong with read-only accesses to read-only
memory...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      04-06-2004
On 5 Apr 2004 15:31:17 GMT, (E-Mail Removed) (Dan Pop) wrote in
comp.lang.c:

> In <c4rlhk$8gh$(E-Mail Removed)> Christopher Benson-Manica <(E-Mail Removed)> writes:
>
> >Wade Yin <(E-Mail Removed)> spoke thus:
> >
> >> How does "abcde" stored in memory?

> >
> >Exactly how is (I believe) up to the implementation, but commonly it
> >ends up in read-only memory.

>
> It's less common than you might think.


....in desk top and workstation type implementations. It is very
common in embedded implementation, so much so in fact that I would
seriously consider rejecting a compiler that copied string literals
from read-only storage to RAM by default.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-06-2004
In <(E-Mail Removed)> Jack Klein <(E-Mail Removed)> writes:

>On 5 Apr 2004 15:31:17 GMT, (E-Mail Removed) (Dan Pop) wrote in
>comp.lang.c:
>
>> In <c4rlhk$8gh$(E-Mail Removed)> Christopher Benson-Manica <(E-Mail Removed)> writes:
>>
>> >Wade Yin <(E-Mail Removed)> spoke thus:
>> >
>> >> How does "abcde" stored in memory?
>> >
>> >Exactly how is (I believe) up to the implementation, but commonly it
>> >ends up in read-only memory.

>>
>> It's less common than you might think.

>
>...in desk top and workstation type implementations.


I.e. in what the standard calls hosted implementations, which happen to
be the default implementations for this newsgroup.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments