944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6910
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 25th, 2006
0

Error:Null Pointer assignment

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Jan 25th, 2006
0

Re: Error:Null Pointer assignment

>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:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 25th, 2006
0

Re: Error:Null Pointer assignment

Also, main really should return an int.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jan 26th, 2006
0

Re: Error:Null Pointer assignment

Why should main return an integer ?
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Jan 26th, 2006
0

Re: Error:Null Pointer assignment

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Jan 26th, 2006
0

Re: Error:Null Pointer assignment

Quote originally posted by comwizz ...
Why should main return an integer ?
http://www.delorie.com/djgpp/v2faq/faq22_25.html
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jan 26th, 2006
0

Re: Error:Null Pointer assignment

The best approach would be to show how you have altered your original code. It should be something like this:
C++ Syntax (Toggle Plain Text)
  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;
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jan 29th, 2006
0

Re: Error:Null Pointer assignment

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Jan 29th, 2006
0

Re: Error:Null Pointer assignment

Heres my attempt for dynamic memory allocation.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Jan 30th, 2006
0

Re: Error:Null Pointer assignment

Please reply . I will have to submit this program tomorrow.
Thanks,
comwizz
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: runtime dll search
Next Thread in C++ Forum Timeline: Any Sample Program???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC