Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote this code:

char * str1="hello world!";
cin>>str1;
cout<<endl<<str1;


But, why does it not work?
Posted
Updated 23-Jun-11 4:17am
v2
Comments
Chris Meech 23-Jun-11 8:23am    
It would be helpful if you could explain what "doesn't work!" means. Do you have compiler errors? Linker errors. When it runs, what do you expect for results?
Reza Oruji 23-Jun-11 8:27am    
no there is no compiler errors,but it doesn't print the value that i input

It doesn't work because str1 is pointing to a Constant string: I.e a read-only string. You cannot modify it!

cin is an input stream - you read from it into teh variable.
cout is an output stream - your write to it from a variable.

If you want to read a string from the user, try this:

char[100] text;
cin >> text;
cout << text << endl;



char * str1="hello world!";
cout<<endl<<str1;
str1 = "hello";
cout<<endl<<str1;
- str1 now is "Hello"



Yes - because you have moved the pointer to a different area of memory.
Instead of referring to a constant string "hello world!" terminated by a null character, it now points to a different constant string "Hello" (terminated by a null character). The original contents of the string are unchanged, you are just pointing at different memory (and you still can't overwrite it!)
Try this:
char* str1="hello world!";
char* str2 = str1;
cout<<endl<<str1;
str1 = "hello";
cout<<endl<<str1;
cout<<endl<<str2;



"I did not try to overwrite it. I just got wrong the line "you can not modify it". More interesting why compiler can not generate any error in this case."

Yes you did! :laugh:
char * str1="hello world!";
cin>>str1;
The cin tries to write whatever the user types into the memory pointed at by str1. This is an write operation, thus an attempt to overwrite the string content.
The compiler will not generate an error - it can't - because there is no way for it to know (in all cases) that the memory is read only:
char * str1="hello world!";
char[100] str2;
GetString(str2);
GetString(str1);
...
void GetString(char* pstr)
   {
   cin >> pstr;
   }

The first call to GetString is fine, the second is a run time error.
 
Share this answer
 
v3
Comments
Sergey Chepurin 23-Jun-11 9:57am    
char * str1="hello world!";
cout<<endl<<str1;

str1 = "hello";

cout<<endl<<str1;
- str1 now is "Hello
OriginalGriff 23-Jun-11 10:10am    
Answer updated.
fjdiewornncalwe 23-Jun-11 10:19am    
Excellent description of what is going on with the pointer. +5
Sergey Chepurin 23-Jun-11 10:37am    
I did not try to overwrite it. I just got wrong the line "you can not modify it". More interesting why compiler can not generate any error in this case.
OriginalGriff 23-Jun-11 10:45am    
Answer updated
It cannot work, because cin is trying to write to a read-only memory area (str1 points to a string literal). You can verify that the following code (pease use short inputs!)
char str1[] = "hello world!";
cin>>str1;
cout<<endl<<str1;

will instead work.

However this is really poor C++ programming: use std::string instead of C-like array of characters, for your strings. e.g.

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
 string str1 = "hello world!";
 cin >> str1;
 cout << endl << str1;
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Jun-11 14:00pm    
Good advice, correct explanation, a 5.
--SA
CSS
char * str1="hello world!";
cin>>str1;
cout<<endl<<str1;



This is the code that you are asking for

let me go line by line


char * str1="hello world!";

in the above statement, str1 is a character pointer which points to the string "hello world!"
this is ok...

now you did
cin>>str1;

this is where you are asking for input from the user
which causes it to force the pointer to act as character variable buffer, which is wrong

ptr1 is a pointer, not a buffer in which you can store your string
moreover as ptr1 is pointer, it can only store addresses not buffer again...

so thats the mistake..


the correct code is..


char* str1 = "hello world!";
// cin>>str1; commented this line as its creating the trouble
cout<<endl<<str1;


====================================================

the other way, if you are asking for input from the user, do it like this...
//just modifying your code a little bit
char[] str1 = "hello world"; //using an array instead of pointer
cin>>str1;
cout<<endl<<str1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900