By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

initialization of char *

P: n/a
Hey,

Who can tell me the difference of these 2 lines:

1. char * str1 = "hello!";
2. char str1[] = "hello!";

thx,
-ditml

Nov 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
char * str1 = "hello!" - should be illegal, right?

Nov 12 '05 #2

P: n/a
ditml wrote:
Hey,

Who can tell me the difference of these 2 lines:

1. char * str1 = "hello!"; here str1 is a pointer into an area of memory, where "hello!" string
resides, it shall not be written (that memory) even though str1 is non const
2. char str1[] = "hello!"; here str1 a is table of 7 char's, the string "hello!" is copied into it,
you may modify contents of str1 freely

thx,
-ditml

Nov 12 '05 #3

P: n/a
* ditml:
char * str1 = "hello!" - should be illegal, right?


Yes, it should, but it isn't, because C++ has to maintain some level of
C compatibility.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 12 '05 #4

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
ditml <li*******@gmail.com> wrote:
Who can tell me the difference of these 2 lines:

1. char * str1 = "hello!";
2. char str1[] = "hello!";


Check out http://www.comeaucomputing.com/techtalk/#ptrvsarray
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 12 '05 #5

P: n/a
In article <11**********************@g47g2000cwa.googlegroups .com>,
ditml <li*******@gmail.com> wrote:
char * str1 = "hello!" - should be illegal, right?


It's a deprecated conversion, so old code protecting us from
asteroids hitting earth will save the day. See the middle of

http://www.comeaucomputing.com/techtalk/#stringliteral
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #6

P: n/a

"Greg Comeau" <co****@panix.com> wrote in message
news:dl**********@panix2.panix.com...
In article <11**********************@g47g2000cwa.googlegroups .com>,
ditml <li*******@gmail.com> wrote:
char * str1 = "hello!" - should be illegal, right?


It's a deprecated conversion, so old code protecting us from
asteroids hitting earth will save the day.


Except that old code wasn't updated with Y2K fixes, so asteroid position
computations will be based on the Earth's position a hundred years ago,
causing us to fire our deflection missiles at innocent alien space tourists
hiding behind the moon. Galactic lawsuits will ensue.

Or perhaps I'm just high...

-Howard

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.