| | |
Pointer question
![]() |
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
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.
Thanks in advance.
C Syntax (Toggle Plain Text)
class LibBook { private: char *name public: LibBook( char* = '\0') void showBookName() };
Thanks in advance.
•
•
•
•
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.
"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
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
I found another example:
C Syntax (Toggle Plain Text)
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; }
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
•
•
•
•
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.
•
•
•
•
Originally Posted by Dave Sinkula
The one you posted is not really correct. The '\0' should be 0.
I got the output "equal" for the following program.
C Syntax (Toggle Plain Text)
#include <cstdio> int main() { if ( '\0' == 0 ) printf( "equal" ); getchar(); }
Or is this only restricted to my compiler (mingw)?
•
•
•
•
Originally Posted by Dante Shamest
Hi Dave, I was under the impression that '\0' was equal to 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
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.
![]() |
Similar Threads
- Mouse PS/2 port no longer working (Windows NT / 2000 / XP)
- This is my first Website. Constructive Criticism Is Welcome! (Website Reviews)
Other Threads in the C Forum
- Previous Thread: Dynamic Array, Writing to CSV, Floating Point ?'s
- Next Thread: Stupid compiler error
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






