Pointer question

Reply

Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Pointer question

 
0
  #1
Aug 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer question

 
0
  #2
Aug 11th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: Pointer question

 
0
  #3
Aug 11th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer question

 
0
  #4
Aug 11th, 2005
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: Pointer question

 
0
  #5
Aug 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: Pointer question

 
0
  #6
Aug 11th, 2005
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.

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)?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: Pointer question

 
0
  #7
Aug 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer question

 
0
  #8
Aug 11th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: Pointer question

 
0
  #9
Aug 11th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC