C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-03-2009, 10:24 PM   #16
Kiss the monkey.
 
CodeMonkey's Avatar
 
Join Date: Sep 2001
Posts: 810
Exactly it.

*i becomes 4, but k remains 3*
__________________
"If you tell the truth, you don't have to remember anything"
-Mark Twain
CodeMonkey is offline   Reply With Quote
Old 01-03-2009, 10:36 PM   #17
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
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?
Ryan0773 is offline   Reply With Quote
Old 01-03-2009, 10:41 PM   #18
Kiss the monkey.
 
CodeMonkey's Avatar
 
Join Date: Sep 2001
Posts: 810
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.
__________________
"If you tell the truth, you don't have to remember anything"
-Mark Twain

Last edited by CodeMonkey; 01-04-2009 at 02:09 AM.
CodeMonkey is offline   Reply With Quote
Old 01-04-2009, 10:21 AM   #19
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Smile

Thanks alot!
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 10:56 AM   #20
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
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.
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 11:23 AM   #21
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
I still dont completely understand how to use pointers. I barely understand what their actual purpose is.
Have you read the pointers tutorial?

Quote:
Originally Posted by Ryan0773
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.
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).

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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-04-2009, 11:35 AM   #22
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Question Laserlight reply

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

Code:
int *pnt = new int;
and

Code:
delete ptr;
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)
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.
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 11:46 AM   #23
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
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)
You can think of it as "creating an object of type int". The pointer named pnt points to this object.

Quote:
Originally Posted by Ryan0773
The second one is supposed to delete a pointer that was allocated with new.
You can think of it as "destroying the object of type int which ptr points to".
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-04-2009, 11:53 AM   #24
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
You can think of it as "creating an object of type int". The pointer named pnt points to this object.
But then how can i give the object of type int a term?

Quote:
Originally Posted by laserlight View Post
You can think of it as "destroying the object of type int which ptr points to".
So this makes the ptr a null pointer?
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 11:58 AM   #25
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
But then how can i give the object of type int a term?
What do you mean?

Quote:
Originally Posted by Ryan0773
So this makes the ptr a null pointer?
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:
Code:
ptr = 0;
or if the <cstddef> header is directly or indirectly included you can also write:
Code:
ptr = NULL;
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-04-2009, 12:08 PM   #26
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
What do you mean?
Like when you write a code that has an int with a certain interger assigned to it

Code:
int x(10);
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)

Quote:
Originally Posted by laserlight View Post
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:

Code:
ptr = 0;
or if the <cstddef> header is directly or indirectly included you can also write:

Code:
ptr = NULL;
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.
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 12:21 PM   #27
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
Like when you write a code that has an int with a certain interger assigned to it
Oh, you can initialise the int with the value of 10 by writing:
Code:
int* ptr = new int(10);
After the object has been created, you can assign the value of 10 to it by writing:
Code:
*ptr = 10;
This latter syntax was covered by the tutorial.

Quote:
Originally Posted by Ryan0773
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.
Yes, and for further reading read Stroustrup's answer to the FAQ: Why doesn't delete zero out its operand?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-04-2009, 12:35 PM   #28
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
This latter syntax was covered by the tutorial.
I'm not sure that it was.

Quote:
Originally Posted by laserlight View Post
Yes, and for further reading read Stroustrup's answer to the FAQ: Why doesn't delete zero out its operand?
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?
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 12:40 PM   #29
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-04-2009, 12:50 PM   #30
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
Just assign the address of x to p.
How would i write that in code would it be something like...

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();
}
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?
Ryan0773 is offline   Reply With Quote
Reply

Tags
basic, basics, c++, lots, noob

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Several Questions, main one is about protected memory Tron 9000 C Programming 3 06-02-2005 07:42 AM
Trivial questions - what to do? Aerie A Brief History of Cprogramming.com 23 12-26-2004 09:44 AM
Questions. anonytmouse A Brief History of Cprogramming.com 4 05-19-2004 02:16 PM
C++ test questions Mister C C++ Programming 9 09-08-2002 12:05 PM


All times are GMT -6. The time now is 11:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22