您好我已经写了以下代码Turbo c++ compiler并尝试打印postfix和中缀,但它正在显示NULL pointer assignment.我不知道为什么会发生这种情况.请帮我....
提前致谢.
#include<iostream.h>
#include<stdio.h>
void main()
{
char *infix,*postfix;
cout<<"Enter postfix exp:";
gets(postfix);
cout<<"Enter infix exp: ";
gets(infix);
cout<<endl<<endl;
puts(postfix);
puts(infix);
}
Run Code Online (Sandbox Code Playgroud)
获取更新的编译器.g ++或VC++都可以.
指针是一个相对棘手的主题.在你正确理解它们如何工作之前,我建议你使用C++的iostream工具和字符串,而不是char数组和C语言stdio.
#include<iostream> // no .h for standard includes
#include <string> // std::string
using namespace std; // to avoid typing std:: in front of everything
int main() // main was never void
{
string postfix, infix;
cout<<"Enter postfix exp:";
cin >> postfix; // read into postfix
cout<<"Enter infix exp: ";
cin >> infix; // read into infix
cout << endl << endl;
cout << postfix << endl; // write postfix followed by a line feed
cout << infix << endl; // write infix followed by a line feed
}
Run Code Online (Sandbox Code Playgroud)
看看它如何更容易?现在您不必担心如何处理内存.