Error:Null Pointer assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Error:Null Pointer assignment

 
0
  #1
Jan 25th, 2006
Hello everyone,
I ahd to make a program for initializing members of a class and displaying them.The members were name , bank account no, account type and current balance.
Well , the program works well except for that null pointer assignment is displayed on the screen after correct output.Could you tell me a reason why this is happening?
Thanks,
comwizz.
  1. /Program to deposit money in a bank account
  2. #include<iostream.h>
  3. #include<string.h>
  4. #include<conio.h>
  5. class account
  6. {
  7. public:
  8. void initialize(char *p,int,char *c,float);
  9. void deposit(float);
  10. void withdraw(float);
  11. void display(void)
  12. {
  13. cout<<"Name: "<<p<<endl;
  14. cout<<"Balance: "<<balance<<endl;
  15. }
  16. private:
  17. float balance,principal;
  18. char *p,*account_type;
  19. int account_no;
  20. };
  21. void account::initialize(char *name,int acc_no,char *acc_type,float initial)
  22. {
  23. int q=strlen(name);
  24. p=new char[q+1];
  25. strcpy(p,name);
  26. account_no=acc_no;
  27. account_type=acc_type;
  28. principal=initial;
  29. balance=initial;
  30. }
  31. void account::deposit(float add)
  32. {
  33. balance=add+principal;
  34. }
  35. void account::withdraw(float subtract)
  36. {
  37. balance=principal-subtract;
  38. }
  39. void main()
  40. {
  41. int i,no;
  42. char *name,*acc_type;
  43. float initial;
  44. account s[2];
  45. for(i=0;i<2;i++)
  46. {
  47. cout<<"Enter name "<<i+1<<": "<<endl;
  48. cin>>name;
  49. cout<<"Enter account no :\n";
  50. cin>>no;
  51. cout<<"Enter account type: \n";
  52. cin>>acc_type;
  53. cout<<"Enter principal amount:\n";
  54. cin>>initial;
  55. s[i].initialize(name,no,acc_type,initial);
  56. }
  57. for(i=0;i<2;i++)
  58. {
  59. s[i].display();
  60. }
  61. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,812
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Error:Null Pointer assignment

 
0
  #2
Jan 25th, 2006
>Could you tell me a reason why this is happening?
name and acc_type are uninitialized pointers in main. You're trying to write to memory that you don't own, and the compiler seems to think that's a bad idea. :rolleyes:
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error:Null Pointer assignment

 
0
  #3
Jan 25th, 2006
Also, main really should return an int.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error:Null Pointer assignment

 
0
  #4
Jan 26th, 2006
Why should main return an integer ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error:Null Pointer assignment

 
0
  #5
Jan 26th, 2006
I defined the characters p1 and p2 and initialized the pointers with their address still the program doesnt work and hangs. What should I modify in my program?
Thanks,
comwizz.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error:Null Pointer assignment

 
0
  #6
Jan 26th, 2006
Originally Posted by comwizz
Why should main return an integer ?
http://www.delorie.com/djgpp/v2faq/faq22_25.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,739
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 281
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Error:Null Pointer assignment

 
0
  #7
Jan 26th, 2006
The best approach would be to show how you have altered your original code. It should be something like this:
  1. int main()
  2. {
  3. int i,no;
  4.  
  5. //name and acc_type have no memory associated with them, so you can't use them yet.
  6. //char *name,*acc_type;
  7.  
  8. //allocate some memory to name---say maybe static memory
  9. char name[80];
  10.  
  11. //allocate some memory to acc_tpe---try dynamic memory if you wish
  12. char * acc_type = new char[80];
  13.  
  14. //now you can put some information in the memory
  15. cin >> name >> endl;
  16. cin >> acc_type >> endl;
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error:Null Pointer assignment

 
0
  #8
Jan 29th, 2006
allocate some memory to acc_tpe---try dynamic memory if you wish.
Without the user specifying the no of letters he is going to enter how should I dynamically allocate memory.for eg. if he is going to enter Dick , I have to allocate 5 bytes without the users specification about the letters he is going to enter i.e. I have to search for a null character and then allocate the memory . I cant figure out how that would be done??
The method you suggested works perfectly fine but would be a wastage of memory as we are taking arrays or allocating memory more than needed.
Thanks,
comwizz.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error:Null Pointer assignment

 
0
  #9
Jan 29th, 2006
Heres my attempt for dynamic memory allocation.
  1. //Program to deposit money in a bank account
  2. #include<iostream.h>
  3. #include<string.h>
  4. #include<conio.h>
  5. class account
  6. {
  7. public:
  8. void initialize(char *p,int,char *c,float);
  9. void deposit(float);
  10. void withdraw(float);
  11. void display(void)
  12. {
  13. cout<<"Name: "<<p<<endl;
  14. cout<<"Balance: "<<balance<<endl;
  15. }
  16. private:
  17. float balance,principal;
  18. char *p,*account_type;
  19. int account_no;
  20. };
  21. void account::initialize(char *name,int acc_no,char *acc_type,float initial)
  22. {
  23. int q=strlen(name);
  24. p=new char[q+1];
  25. strcpy(p,name);
  26. account_no=acc_no;
  27. account_type=acc_type;
  28. principal=initial;
  29. balance=initial;
  30. }
  31. void account::deposit(float add)
  32. {
  33. balance=add+principal;
  34. }
  35. void account::withdraw(float subtract)
  36. {
  37. balance=principal-subtract;
  38. }
  39. void main()
  40. {
  41. int i,no,count=0;
  42. char *name,*acc_type,*p;
  43. float initial;
  44. account s[2];
  45. for(i=0;i<2;i++)
  46. {
  47. cout<<"Enter name "<<i+1<<": "<<endl;
  48. cin>>p;
  49. while(*p!='\0')
  50. {
  51. count++;
  52. p++;
  53. }
  54. name=new char[count+1];
  55. name=p;
  56. cout<<"Enter account no :\n";
  57. cin>>no;
  58. cout<<"Enter account type: \n";
  59. char *q;
  60. cin>>q;
  61. count=0;
  62. while(*q!='\0')
  63. {
  64. count++;
  65. q++;
  66. }
  67. acc_type=new char[count+1];
  68. acc_type=q;
  69. cout<<"Enter principal amount:\n";
  70. cin>>initial;
  71. s[i].initialize(name,no,acc_type,initial);
  72. }
  73. for(i=0;i<2;i++)
  74. {
  75. s[i].display();
  76. }
  77. }
Thanks,
comwizz.
Last edited by comwizz; Jan 29th, 2006 at 12:43 am. Reason: mistake in code
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error:Null Pointer assignment

 
0
  #10
Jan 30th, 2006
Please reply . I will have to submit this program tomorrow.
Thanks,
comwizz
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC