0

Hi I have written the following code in Turbo c++ compiler and try to print postfix & infix, but it is displaying NULL pointer assignment. I m not getting why it is happening. Please help me....

Thanks in advance.

#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);
}
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
ARYAN
  • 1
  • 1
  • 15
    A few suggestions: (1) You should probably get a newer compiler; Visual C++ Express and g++ are both freely available. (2) Make sure that you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). (3) **Never ever use `gets`. Never ever.** It cannot be used safely; any program that uses `gets` has a security bug. – James McNellis Apr 20 '11 at 17:48
  • this is not an interactive tutor site. You are supposed to do your homework yourself. – Nikolai Fetissov Apr 20 '11 at 17:50
  • Man page for gets `4.38 gets--get character string (obsolete, use fgets instead)` – Martin York Apr 20 '11 at 18:07
  • @Martin also the latest revision of the C standard, ISO 9899:TC3 7.26.9/2 "The gets function is obsolescent, and is deprecated" – Cubbi Apr 20 '11 at 19:10
  • so, question was: why am i getting error NULL POINTER ASSIGMENT. you receive it because char *infix, *postfix are pointers, which are undefined. you didn't define them. so they may point to random part of memory. it's very good that compiler can notify you about you otherwise you might break some important part of memory. it's like to define int a; and afterward try to output this, you variable is undefined and you ll receive gibberish. – Tebe Sep 26 '11 at 21:16

6 Answers6

7

Get a more recent compiler. Either g++ or VC++ will be fine.

Pointers are a relatively tough subject. Until you properly understand how they work, I suggest you use C++'s iostream facilities along with string, instead of char arrays and C's 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
}

See how it's easier? Now you don't have to worry about how memory is handled.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
2
char *infix,*postfix;

These're pointers which has not been allocated memory.

Declare them as array so that you don't need to allocate memory (and deallocate it once done):

char infix[1024]; 
char postfix[1024];

Or better yet, use std::string, std::cin and std::cout etc.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
-1

You have to initialize infix and postfix with memory. Something like:

char *infix,*postfix;
infix = new char[20];
postfix = new char[20];

The size with which you will initialize will depend upon your requirement. I have just picked a random number as 20.

Aamir
  • 14,882
  • 6
  • 45
  • 69
-1

I agree with the comments. But to answer the question directly, the infix and postfix variables start out as NULL. You can allocate them on the stack, or on the heap. Here's heap allocation. At some point when the buffers are no longer needed, you will need to call delete[].

 char *infix,*postfix;
 infix = new char[50];
 postfix = new char[50];

 cout<<"Enter postfix exp:";
 gets(postfix);
 cout<<"Enter infix exp: ";
 gets(infix);
  cout<<endl<<endl;
 puts(postfix);
 puts(infix);
mdma
  • 56,943
  • 12
  • 94
  • 128
  • you're of course right, and that how it is in C/C++ - auto variables start out uninitialized, but since the OP is getting a `NULL pointer assignment` I was thinking that turbo C++ might be different. – mdma Apr 20 '11 at 17:58
  • 1
    -1 for copying the OP's code but not correcting his use of `gets`. – James McNellis Apr 20 '11 at 18:01
  • Sure. it's not ideal but it will work. "Can you correct the rest of my code" wasn't part of the question asked. – mdma Apr 20 '11 at 18:05
-1

just off the top of my head I would say its because you are assigning to pointers which have had no memory allocated to them.

try with something like:

 char infix[255],postfix[255]

or do what you have but allocate some memory to the pointers first:

 char* infix = new char[255];
 char* postfix = new char[255];
 free infix;
 free postfix;
Stewart Dale
  • 361
  • 1
  • 5
  • 12
  • 1
    -1 for suggesting dynamic allocation with manual memory management and not explaining that the user is then responsible for cleaning up the dyanmically allocated memory. – James McNellis Apr 20 '11 at 18:05
-1

You haven't assigned any memory for *infix and *postfix. All you have done is create pointers to memory locations. The program has no idea how much memory to allocate. You will have to give them a default size: char *infix = new char[256]; or some other size value.

Jess
  • 2,991
  • 3
  • 27
  • 40
  • 2
    -1 for suggesting dynamic allocation with manual memory management and not explaining that the user is then responsible for cleaning up the dyanmically allocated memory. – James McNellis Apr 20 '11 at 18:05