Sign in to follow this  
MannyZanny

Pointers

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Do yourself a HUGE favor:
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 //

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

class ClassA
{
public:
ClassA()
{
m_number = 5;
}
int m_number;
}
class ClassB
{
public:
ClassB()
{
theint = -1;
}
void GetANum(ClassA *ClassPointer);
int theint;
}

ClassB::GetANum(ClassA *ClassPointer)
{
theint = ClassPointer->m_number;
}

main()
{
ClassA MyClassA;
ClassB MyClassB;

MyClassB.GetANum(&MyClassA);

printf("%d", MyClassB.theint);
}


Obviously this is psuedocode and won't compile, but it gets the point across I think. It would print the number 5 assuming that it had the proper includes, etc.

You really should read more about pointers, they are one of the harder concepts to grasp.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this