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

问题描述

限时删除!!

我写了这段代码:

I wrote this code:

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



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



But, why does it not work?

推荐答案

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.


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 *变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 08:02