如何在cin中使用char *变量? [英] how can i use char * variable in cin ?

查看:167
本文介绍了如何在cin中使用char *变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

I wrote this code:

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



但是,为什么它不起作用?



But, why does it not work?

推荐答案

它不起作用,因为str1指向常量字符串:即只读字符串.您无法修改!

cin是输入流-您可以从中读取变量.
cout是输出流-您从变量对其进行写入.

如果要从用户读取字符串,请尝试以下操作:

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现在是"Hello"



是的-因为您已将指针移至其他内存区域.
而不是引用常量字符串"hello world!"现在以空字符终止,它指向另一个常量字符串"Hello"(以空字符终止).字符串的原始内容保持不变,您只是指向不同的内存(您仍然无法覆盖它!)
试试这个:




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;




我没有尝试覆盖它.我只是在您不能修改它"这一行弄错了.更有趣的原因是在这种情况下编译器不能生成任何错误."

是的,你做到了! :laugh:




"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;

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;
   }


第一次调用GetString很好,第二次调用是运行时错误.


The first call to GetString is fine, the second is a run time error.


它不起作用,因为cin试图写入只读存储区(str1指向字符串文字).您可以验证以下代码(请使用简短的输入!)
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;


相反,它将起作用.

但是,这真的是很差的C++编程:将std::string而不是类似C的字符数组用作字符串.例如


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.

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

int main()
{
 string str1 = "hello world!";
 cin >> str1;
 cout << endl << str1;
}


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




这是您要的代码

让我逐行走


char * str1 ="hello world!";

在上面的语句中,str1是一个字符指针,它指向字符串"hello world!".
没关系...

现在你做了
cin>> str1;

这是您要求用户输入的地方
这导致它强制指针充当字符变量缓冲区,这是错误的

ptr1是一个指针,而不是一个可以在其中存储字符串的缓冲区
此外,由于ptr1是指针,它只能存储地址,不能再次缓冲...

多数民众赞成在错误..


正确的代码是..


char * str1 =世界你好!";
//cin>> str1;评论此行是造成麻烦的原因
cout<< endl<< str1;


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

反之,如果您要求用户输入,请像这样...
//只是稍微修改一下代码
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;


这篇关于如何在cin中使用char *变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆