944,078 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2455
  • C RSS
Aug 11th, 2005
0

Pointer question

Expand Post »
Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.

  1.  
  2. class LibBook
  3.  
  4. {
  5. private:
  6. char *name
  7. public:
  8. LibBook( char* = '\0')
  9. void showBookName()
  10.  
  11. };

Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Aug 11th, 2005
0

Re: Pointer question

Quote originally posted by Jon182 ...
Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.
Generally you do. Do you have a more specific example? The one you posted is not really correct. The '\0' should be 0.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 11th, 2005
0

Re: Pointer question

I found another example:

  1.  
  2. class Date
  3. {
  4. private:
  5. char day[10];
  6. char month[10];
  7. int year;
  8.  
  9. public:
  10.  
  11.  
  12. void set(char* day, char* month, int year);
  13.  
  14. void display(Date datem);
  15.  
  16. char* get_day();
  17.  
  18. char* get_month();
  19.  
  20. int get_year();
  21.  
  22. };
  23.  
  24. void Date::set(char* d, char* m, int y)
  25. {
  26.  
  27. strncpy(day, d, 9);
  28. strncpy(month, m, 9);
  29. year=y;
  30.  
  31. }
  32.  
  33.  
  34. void Date::display(Date date)
  35. {
  36. cout << "The day is ";
  37. cout << date.get_day();
  38.  
  39. cout << "\nThe month is ";
  40. cout << date.get_month();
  41.  
  42. cout << "\nThe year is ";
  43. cout << date.get_year();
  44. cout << endl;
  45. }
  46.  
  47. char* Date::get_day()
  48. {
  49. return day;
  50. }
  51.  
  52. char* Date::get_month()
  53. {
  54. return month;
  55. }
  56.  
  57. int Date::get_year()
  58. {
  59. return year;
  60. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Aug 11th, 2005
0

Re: Pointer question

What is your question in regard to pointers about your last posted code? The functions that return a pointer to the beginning of a char array that is private? Or the function parameters that receive a pointer to the beginning of a string to copy?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 11th, 2005
0

Re: Pointer question

I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Aug 11th, 2005
0

Re: Pointer question

Quote originally posted by Jon182 ...
I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.
The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.

Quote originally posted by Dave Sinkula ...
The one you posted is not really correct. The '\0' should be 0.
Hi Dave, I was under the impression that '\0' was equal to 0?
I got the output "equal" for the following program.
  1. #include <cstdio>
  2.  
  3. int main()
  4. {
  5. if ( '\0' == 0 )
  6. printf( "equal" );
  7. getchar();
  8. }

Or is this only restricted to my compiler (mingw)?
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
Aug 11th, 2005
0

Re: Pointer question

Quote originally posted by Dante Shamest ...
The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.
Thanks for your help Dave & Dante.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Aug 11th, 2005
0

Re: Pointer question

Quote originally posted by Dante Shamest ...
Hi Dave, I was under the impression that '\0' was equal to 0?
It was more of a hint not to get into the habit of writing characters to pointers and confusing NULL and '\0'.

[edit]So yes, I overstated things when I said it wasn't quite correct.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 11th, 2005
0

Re: Pointer question

Ah I see. Yes, one should generally initialize pointers to 0 instead of '\0' to avoid confusion. The Great One himself recommends it as well.
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003

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: Dynamic Array, Writing to CSV, Floating Point ?'s
Next Thread in C Forum Timeline: Stupid compiler error





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


Follow us on Twitter


© 2011 DaniWeb® LLC