char [ ] and char *

B

bintom

Why is it the this code runs:

char str1[] = "Prof. Vally";
cout << ++(*str1) << (*str1)++;


But this code crashes:

char *str2 = "Prof. Vally";
cout << ++(*str2) << (*str2)++;


I use the Dev C++ compiler
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Why is it the this code runs:

char str1[] = "Prof. Vally";
It stores a copy of "Prof. Vally" in RW array, that's OK
cout << ++(*str1) << (*str1)++;


But this code crashes:

char *str2 = "Prof. Vally";
This is deprecated, don't use.
str2 points to a read-only copy of "Prof. Vally", therefore, it crashes.
cout << ++(*str2) << (*str2)++;


I use the Dev C++ compiler
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAkzw/7EACgkQZ1Turg5KUCkDHgCePIJXyJ4Fi2OUyA2MO82z6aZ6
ydAAnjhDHM2/JKkK0CaOrzw6eOu+QHi7
=qEMJ
-----END PGP SIGNATURE-----
 
Ö

Öö Tiib

Why is it the this code runs:

char str1[] = "Prof. Vally";
  cout << ++(*str1) << (*str1)++;

But this code crashes:

  char *str2 = "Prof. Vally";
  cout << ++(*str2) << (*str2)++;

I use the Dev C++ compiler

Because char array and pointer to string literal are different things.
It is illegal to attempt to modify chars in string literal.
 
B

bintom

Why is it the this code runs:
char str1[] = "Prof. Vally";
cout << ++(*str1) << (*str1)++;

But this code crashes:
char *str2 = "Prof. Vally";
  cout << ++(*str2) << (*str2)++;

I use the Dev C++ compiler
Because char array and pointer to string literal are different things.
It is illegal to attempt to modify chars in string literal.


Thanks Michael and Oo Tiib. I wish the many text books that I have
referred would have mentioned this. It would have saved a lot of
time.

Further, when I print the address of str1, I get 2359136
whereas, for the address of str2, I get 4456462

Does that mean that str1 is allocated in the stack and str2 in the
heap?

Thanks again
 
Ö

Öö Tiib

Why is it the this code runs:
  char str1[] = "Prof. Vally";
  cout << ++(*str1) << (*str1)++;

But this code crashes:
  char *str2 = "Prof. Vally";
  cout << ++(*str2) << (*str2)++;

I use the Dev C++ compiler
Because char array and pointer to string literal are different things.
It is illegal to attempt to modify chars in string literal.

Thanks Michael and Oo Tiib. I wish the many text books that I have
referred would have mentioned this. It would have saved a lot of
time.

Further, when I print the address of str1, I get 2359136
         whereas, for the address of str2, I get 4456462

Does that mean that str1 is allocated in the stack and str2 in the
heap?
Why is it the this code runs:
  char str1[] = "Prof. Vally";
  cout << ++(*str1) << (*str1)++;

But this code crashes:
  char *str2 = "Prof. Vally";
  cout << ++(*str2) << (*str2)++;

I use the Dev C++ compiler
Because char array and pointer to string literal are different things.
It is illegal to attempt to modify chars in string literal.

Thanks Michael and Oo Tiib. I wish the many text books that I have
referred would have mentioned this. It would have saved a lot of
time.

Further, when I print the address of str1, I get 2359136
         whereas, for the address of str2, I get 4456462

Does that mean that str1 is allocated in the stack and str2 in the
heap?

There are no stack and heap in C++. There are objects and underlying
memory storage. The storage duration can be 'automatic', 'static' and
'dynamic'. Local char array has automatic storage, string literal is
constant in static storage.

It is unneeded complexity so most competent programmers consider char*
usage for text processing as defect. Usual suggestion is to use
std::string or other text-containing class instead. Then you can
hopefully start to think about problems that you solve and not
underlying memory addresses.
 
P

Paul N

Why is it the this code runs:
  char str1[] = "Prof. Vally";
  cout << ++(*str1) << (*str1)++;

But this code crashes:
  char *str2 = "Prof. Vally";
  cout << ++(*str2) << (*str2)++;

I use the Dev C++ compiler
Because char array and pointer to string literal are different things.
It is illegal to attempt to modify chars in string literal.

Thanks Michael and Oo Tiib. I wish the many text books that I have
referred would have mentioned this. It would have saved a lot of
time.

The C++ language is based on the C language, and this question is
number 1.32 (and 8.5) in the C FAQ. It may be worthwhile having a look
through the C FAQ at http://c-faq.com/ as well as the C++ FAQ at
http://www.parashift.com/c++-faq-lite/ particularly if you are going
to be using the C-like bits of C++. (For many things, the C way of
doing it is messy, and while it works in C++, C++ also provides easier
ways of going about it. For instance, most people would suggest using
a C++ string rather than a char array to handle strings. Though I
still do things the hard way myself.)

Hope that helps.
Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top