Exactly it.
*i becomes 4, but k remains 3*
Exactly it.
*i becomes 4, but k remains 3*
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Ok so when using i++, the int k will become what i was but then i will change and the k will not change because the post-increment makes it so that only the i changes. But the pre-increment makes the i change before the int k makes itself equal to i so they would both be the same.(Does that make sense?)
PS: This is completely unrelated to anything we were talking about but i was just wondering, how do you get a signature?
That's one way to think about it. Think about it more literally and it might be simpler.
++i adds one to i and returns i
i++ returns a temporary copy of i and then adds one to the real i.
*edit* "user cp" for the signature.
Last edited by CodeMonkey; 01-04-2009 at 02:09 AM.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
I still dont completely understand how to use pointers. I barely understand what their actual purpose is. Apparently they help to make sure that you have enough space for your program and that you arent wasting any space either. Please help, the whole purpose of pointers just isnt clear. How are they supposed to help your program? when should you use them? HOW do you use them? They just dont seem to make any sense.
Have you read the pointers tutorial?Originally Posted by Ryan0773
Well, one use of pointers is to avoid copying an expensive to copy object when you want to pass that object around. However, in C++ this use is largely replaced by references, unless you also want to make passing an object optional (i.e., the caller can pass a null pointer instead of a pointer to an object).Originally Posted by Ryan0773
Another use would be to manage a dynamically allocated array, or to create links between the nodes of a linked list. However, this use is largely replaced by containers that abstract away the use of pointers.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, i did read the tutorial. I can sortof make a basic pointer that points to another integer. but whats the purpose of codes like
andCode:int *pnt = new int;
The first one is supposed do something like take a free pointer and make it point to something that becomes unavailable to other programs or something (dont really understand what the means)Code:delete ptr;
The second one is supposed to delete a pointer that was allocated with new. Thats doesn't really help me if I dont understand the purpose of new.
You can think of it as "creating an object of type int". The pointer named pnt points to this object.Originally Posted by Ryan0773
You can think of it as "destroying the object of type int which ptr points to".Originally Posted by Ryan0773
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What do you mean?Originally Posted by Ryan0773
No, ptr merely points to a destroyed object and hence may not be dereferenced unless and until you make it point to an object that exists. To set ptr to be a null pointer after the object that it points to is destroyed you would write:Originally Posted by Ryan0773
or if the <cstddef> header is directly or indirectly included you can also write:Code:ptr = 0;
Code:ptr = NULL;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Like when you write a code that has an int with a certain interger assigned to it
Make it so that x has a value of ten. When i make a pointer point to something else, how can i make it so that the int that it points to has a value (srry i think i might have used the wrong word to explain it)Code:int x(10);
So if i want to make a pointer null (or point at 0 or nothing) then i would have to do it manually by inserting the code. it wouldn't do it by itself.
Oh, you can initialise the int with the value of 10 by writing:Originally Posted by Ryan0773
After the object has been created, you can assign the value of 10 to it by writing:Code:int* ptr = new int(10);
This latter syntax was covered by the tutorial.Code:*ptr = 10;
Yes, and for further reading read Stroustrup's answer to the FAQ: Why doesn't delete zero out its operand?Originally Posted by Ryan0773
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm not sure that it was.
Alright ill check it out,thanks. But does is there a way that i can change the characteristics of a certain pointer like *p which point to int i, could i change it so that later in the script *p would go from i to x?
Just assign the address of x to p.Originally Posted by Ryan0773
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
How would i write that in code would it be something like...
I have no idea if this would work or not. Can you tell me if i made any small errors or if i did it completely wrong?Code:{ int i; int x; int *p p = &i cout<< *p <<"\n"; int *p = new int(x); cout<< *p <<"\n"; delete p; *p = 0; cin.get(); }