Pointers
I have had a huge amount of trouble getting pointers to work. I made a simple dialog that increases and decreases a variable when a button is pressed. Another window is created and is supposed to retrieve the value but it doesnt work. The project is at http://home.cogeco.ca/~manuelmarquez/Pointer Test.rar. I would greatly appreciate it if you download the file and figure out whats wrong with it and email it back to me at manuelmarquez@cogeco.ca. THANKS A LOT.
I doubt anyone will go through your project and fix your bugs. The best thing to do is isolate the problem, find whatever you can about the most confusing aspects from google and self experimentation. And then come here with some code snippits, and some detailed questions. You'll get a lot more help.
Do yourself a HUGE favor:
Don't use MFC until you learn C++.
You weren't using any pointers.
Don't use MFC until you learn C++.
You weren't using any pointers.
CPointerTestDlg Pointer; // not a pointer, but an // // instance of the class.//CPointerTestDlg *Pointer; // THIS IS A POINTER //
OK i did this
CPointerTestDlg *Pointer;
m_sNumber = Pointer->m_sNumber;
But now the program crashes and a warning comes up saying Pointer used without being initialized?
CPointerTestDlg *Pointer;
m_sNumber = Pointer->m_sNumber;
But now the program crashes and a warning comes up saying Pointer used without being initialized?
I can answer your question,but I won't,because I'm mean.No,really get a C++ book,read a tutorial on the net about pointers,anything.You don't know the first thing about them,so giving you a quick answer won't help you at all.Pointers is one of the most difficult parts of programming,you have to learn it the hard way.
-EDIT:Ok,I'm not that mean,so I'll answer you:
-EDIT2:Sorry,my evil side took over again,and I deleted the answer.It wouldn't help you understanding pointers anyway.
[Edited by - mikeman on July 8, 2004 2:04:15 PM]
-EDIT:Ok,I'm not that mean,so I'll answer you:
-EDIT2:Sorry,my evil side took over again,and I deleted the answer.It wouldn't help you understanding pointers anyway.
[Edited by - mikeman on July 8, 2004 2:04:15 PM]
Quote:Original post by MannyZanny
OK i did this
CPointerTestDlg *Pointer;
m_sNumber = Pointer->m_sNumber;
But now the program crashes and a warning comes up saying Pointer used without being initialized?
I have sympathy [razz]
quick lesson, then you do what mikeman said and read a book on C++.
...to be continued
edit: conclusion.
This thread on pointers should answer most of your questions. i and some others go through pointers with very much detail. you should have a basic understanding by the time you thoroughly go through this thread. make sure you have your compiler up and running to try out the different code presented in that thread. read and not coding will not make you learn any faster. trust me.... i've been there.
anyway, see you in 3 days.
and it better be 3 days [evil] [flaming]
OK Now i can get the address but it crshes when i try to access the value. this is the code:
CPointerTestDlg *Pointer = NULL;
int *Addr = &Pointer->m_iNumber;
m_sNumber.Format("%d", *Addr);
UpdateData(FALSE);
Error at *Addr. HELP!
CPointerTestDlg *Pointer = NULL;
int *Addr = &Pointer->m_iNumber;
m_sNumber.Format("%d", *Addr);
UpdateData(FALSE);
Error at *Addr. HELP!
CPointerTestDlg *Pointer = NULL;
Ok. But you need to have the pointer point at something (as opposed to "nothing", which is what NULL conceptually is) before you can dereference it (i.e. use the pointer).
int *Addr = &Pointer->m_iNumber;
(a) Did you try "&(Pointer->m_iNumber)"? I suspect the default precedence is the other way around, though I'm not sure.
(b) You don't really need to take an address here and then dereference it again in the next line. What you're doing is making another pointer that points into the middle of the CPointerTestDlg, and accessing the value through that. You could just as easily make a copy of the value itself.
(c) Most importantly: You can't do this while Pointer is still pointing at NULL. There has to be a CPointerTestDlg to refer to, in order to use its data.
Ok. But you need to have the pointer point at something (as opposed to "nothing", which is what NULL conceptually is) before you can dereference it (i.e. use the pointer).
int *Addr = &Pointer->m_iNumber;
(a) Did you try "&(Pointer->m_iNumber)"? I suspect the default precedence is the other way around, though I'm not sure.
(b) You don't really need to take an address here and then dereference it again in the next line. What you're doing is making another pointer that points into the middle of the CPointerTestDlg, and accessing the value through that. You could just as easily make a copy of the value itself.
(c) Most importantly: You can't do this while Pointer is still pointing at NULL. There has to be a CPointerTestDlg to refer to, in order to use its data.
How come this crashes on delete?
CPointerTestDlg *Pointer = new CPointerTestDlg;
m_sNumber = Pointer->m_sNumber;
UpdateData(FALSE);
delete Pointer;
CPointerTestDlg *Pointer = new CPointerTestDlg;
m_sNumber = Pointer->m_sNumber;
UpdateData(FALSE);
delete Pointer;
err ..
are you trying to say:
?
Quote:m_sNumber = Pointer->m_sNumber;
are you trying to say:
Pointer->m_sNumber = m_sNumber;
?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement