473,399 Members | 2,774 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,399 software developers and data experts.

initialization of char *

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
6 3116
char * str1 = "hello!" - should be illegal, right?

Nov 12 '05 #2
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
* 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
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
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

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

Similar topics

3
by: Fixa | last post by:
I have a problem with reference initialization. Maybe i shom my problem on simple example: class foo { private: ... public: virtual void fun(void);
2
by: Kris | last post by:
Hi All, I just tried to do something that I thought would be quite simple in C++ and discovered (I think) that it's not possible. I did a bunch of reading and everything that I've seen seems to...
13
by: Nolan Martin | last post by:
How would I go about calling a function before the main function is called? The function needs to be called multiple times from different locations. I have something like this working but it is...
18
by: Michael Press | last post by:
Hello. I am puzzled. A line of the form char array = { a}; or char array = { a, b, c}; is an array initializer.
5
by: aarklon | last post by:
Hi all, why the following structure initialization is not valid #include<stdio.h> struct rec { char name; int age; };
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
5
by: sarathy | last post by:
Consider the code below. char a="Hello"; char b="Hello"; printf ("%d\n",sizeof(a)); printf ("%d\n",sizeof(b));
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
5
by: codeGhost | last post by:
I've been trying to ignore this issue for a while now, but I've come to the point in my code where I can't do so anymore. (For those of you who are wondering, this is NOT a homework question). ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.