Advertisement

Newbie here..why isn't this working?

Started by March 13, 2003 12:02 AM
20 comments, last by Daerus 21 years, 8 months ago
just thought thatd id mention that the protected memory model should keep one program from screwing up anothers memory no matter what you do
the operative word is SHOULD
particularly in the Win9x period (which made notoriously bad use of the protected memory model) you could do things that messed with everything, generally leading to a blue screen, but in generally all youll screw up is yourself
-PoesRaven
PoesRaven, you''re right. But just to clarify: using an uninitialized pointer is a _bad_ thing. Hopefully we''re all striving for reasonably good code, so how about we all just initialize everything ''n try to get along?

Big smiley goes here.
Advertisement


int *ptr;

The above is a pointer pointing nowhere (or pointed at something you do not have access to).

To allocate space for you pointer do this:

ptr = new int;

Now you can copy the value of a to the location ptr is pointing to:

*ptr = a;

When before shutting down your program remeber to free the location again like this:

delete ptr;


Remember, use new when you must create a new place to store data.
If you want to point to existing data you do not need to new.

int a;
int *ptr;

ptr = &a;

The pointer ptr now points to the location where a stores it''s data.

If you change a like this:

a = a + 7;

Then the value which ptr is pointing to is also changed.

value stored at *ptr is equal to value of a.






i agree totally Melraiden
just thought id mention some low level info for the curious, although your not mentioning it is fine for people who dont need to know it -- i routinely dont tell beginners and noncomputer types the whole truth if i can avoid it -- its frustrating when the glazed eyes appear

[insert smily as well]
-PoesRaven
Dont trip out over pointers, I think if I''m correct they are mainly used in passing the location of data instead of passing the data itself to another function. Make an app a bit quicker if I remember correctly. But, dont believe anything I say at the moment unless someone else confirms it because it''s been awhile since I''ve gone through this.
Thanks for all the help with pointers, it's helped me a lot! If you guys don't mind, I have another question about this little snippet of code from a pointers tutorial:

for (ii=0; ii < 100; ii++{     *pt4 = *pt3;            // The contents of the address contained in pt3 are assigned to            // the contents of the address contained in pt4     pt4++;     pt3++;}  


First, it's about the for (ii=0; ii < 100; ii++, I understand what's going on here, but what I don't understand is what the ii is for, nowhere in the code on the website is ii defined as a variable or anything, I'm confused as to where it came from.

Also, with the pt4++; and pt3++;.. what exactly do the '++' do with these?

Sorry for the (most likely) very newbie questions.. just trying to increase my knoweledge of C/C++, and trying to get some questions answered that aren't explained with the tutorials that I have seen.

Thank you very much (again) for any help!

Edit - I don't think my source tags worked.. its [ source ] and [ / source ], except without spaces, correct?

[edited by - Daerus on March 13, 2003 2:06:16 AM]
Advertisement
Also you should use iostream instead of iostream.h, std::cout instead of cout, and std::endl instead of endl.

It seems like I type this every day! :-)

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

  for (ii=0; ii < 100; ii++){            *pt4 = *pt3;       pt4++;     // same as pt4 = pt4 + 1;       pt3++;}   


when it is a code snippet, you can''t see the full source of the program, is only a part of it

so it is valid to assume that the variable ii is declare somewhere else!
Nothing to do, try complimenting someone, it will make you feel better!
Yes, I understand what a snippet is . But when I went through the rest of the source code on the tutorial site, I didn''t see that they defined ''ii'' anywhere, that''s why I was curious. Perhaps I''m just mistaken and overlooked the code.

Thanks for your help though!
The ++ operator is an increment operator, it increments(adds) the value by 1.

say if you have something like this

int main()
{
int i;
int z;

i=4;
z=0;

z=i++;
}

what this does is assign the value of i to z, so the variable z now stores the value 4 . then the variable i is incremented by 1, so i will now be 5. so now i is 5 and z is 4.

but if it was written like this:
z=++i;

what this does is increment i first, then assigns it to z. ok so i is incremented first to make it 5, then it is assigned to z, so now i and z are both 5.

the same is true for the decrement operator --

hope that helped.

This topic is closed to new replies.

Advertisement