NULL指针赋值错误

0 c++ turbo-c++

您好我已经写了以下代码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)

Eti*_*tel 7

获取更新的编译器.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)

看看它如何更容易?现在您不必担心如何处理内存.

  • 你需要使用`std :: getline`来匹配`std :: gets`的行为,并且一些错误检查可能是有序的,但是**谢谢**因为没有建议手动内存管理. (4认同)