-
C (
http://www.daniweb.com/forums/forum118.html)
| Jon182 | Aug 11th, 2005 1:43 pm | |
| Pointer question 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.
class LibBook
{
private:
char *name
public:
LibBook( char* = '\0')
void showBookName()
};
Thanks in advance. |
| Dave Sinkula | Aug 11th, 2005 1:47 pm | |
| 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. |
| Jon182 | Aug 11th, 2005 1:58 pm | |
| Re: Pointer question I found another example:
class Date
{
private:
char day[10];
char month[10];
int year;
public:
void set(char* day, char* month, int year);
void display(Date datem);
char* get_day();
char* get_month();
int get_year();
};
void Date::set(char* d, char* m, int y)
{
strncpy(day, d, 9);
strncpy(month, m, 9);
year=y;
}
void Date::display(Date date)
{
cout << "The day is ";
cout << date.get_day();
cout << "\nThe month is ";
cout << date.get_month();
cout << "\nThe year is ";
cout << date.get_year();
cout << endl;
}
char* Date::get_day()
{
return day;
}
char* Date::get_month()
{
return month;
}
int Date::get_year()
{
return year;
}
|
| Dave Sinkula | Aug 11th, 2005 2:04 pm | |
| 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? |
| Jon182 | Aug 11th, 2005 2:10 pm | |
| 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. |
| Dante Shamest | Aug 11th, 2005 2:32 pm | |
| 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.
#include <cstdio>
int main()
{
if ( '\0' == 0 )
printf( "equal" );
getchar();
}
Or is this only restricted to my compiler (mingw)? |
| Jon182 | Aug 11th, 2005 2:40 pm | |
| 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. |
| Dave Sinkula | Aug 11th, 2005 3:12 pm | |
| 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. |
| Dante Shamest | Aug 11th, 2005 3:37 pm | |
| Re: Pointer question |
| All times are GMT -4. The time now is 1:36 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC