如何分配char指针和cin? [英] How do I assign a char pointer with cin?
本文介绍了如何分配char指针和cin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以基本上当我尝试这样做
So basically when I try to do this
char* inputFileName;
cout<< "Filename: "; cin>>*inputFileName;
它可以让我输入文件名,但是当我按enter键得到一个未处理的异常错误。有任何想法吗?
it lets me input the filename but when I press enter I get an unhandled exception error. Any ideas?
也可以编辑。
char* inputFileName;
cout<< "Filename: "; cin>>inputFileName;
我尝试运行时调试声明失败。
i get a debug asseration failed when i try to run it.
推荐答案
您需要传入指针,而不是引用指针,并为char *分配内存。
You need to pass in the pointer, not the dereferenced pointer, and have allocated memory for the char *
#include <iostream>
using namespace std;
int main(){
char inputFileName[1024];
cout << "Filename: ";
cin >> inputFileName;
cout << inputFileName << endl;
}
char inputFileName [1024] / code>是
Another option besides char inputFileName[1024];
is
char *inputFileName;
inputFileName = new char[BUFFER_SIZE];
BUFFER_SIZE当然和以后定义
with BUFFER_SIZE defined somewhere of course, and later
delete [] inputFileName;
这篇关于如何分配char指针和cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文