Purpose of Pointers?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2004
Posts: 150
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Purpose of Pointers?

 
0
  #1
Jan 3rd, 2005
I have been reading about pointers, which IS confusing. I'm sure there is a purpose, but I don't know why I need them yet. For example

var1 = 23;        // assigning 23 to var1
var2 = var1;      // assigning the value of var1 to var2
var3 = &var1;     // assigning the address of var1 to var3

I think I might know what pointers do. I don't, however, know what the practical use of a pointer is. From what I've learned about pointes, is that they directly give the value of whats at that location in memory.

My question is: what is the practical use of pointers?

Examples && Explanations = *Welcomed :cheesy:
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 13
Reputation: kaiser<lucy> is an unknown quantity at this point 
Solved Threads: 1
kaiser<lucy> kaiser<lucy> is offline Offline
Newbie Poster

Re: Purpose of Pointers?

 
0
  #2
Jan 3rd, 2005
i havent slept in days and i hope i dont say something completely stupid here:

pointers contain the memory location of a variable. one of the reasons that this is useful concerns memory efficiency. if you're passing a variable to a function then the function has to make another copy of that variable and if that variable is an object of type NUCLEAR_REACTOR_PLAN, then you might run out of memory mighty fast. but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: Purpose of Pointers?

 
0
  #3
Jan 4th, 2005
I got the similar problem with you, Geek-Master. What kaiser<lucy> said is still a little bit abstract for me.

So Geek-Master have you tried the dynamic memory allocation using new and delete operator? They could only deal with pointers.

Now my own problem comes. Books say we can use new operator to reserve memory for array of unknown size like this:
<code>
int* ptr = new int[100]; //the array ptr has interger type and 100 elements
</code>

Now another version:
<code>
int n;
int ptr[n];

cin >> n; //ask user to enter size of array, now array has size
</code>

So what is the difference between the two versions of array declaration?
In both cases the array is declared but doesn't have defined-size unless it is determined during runtime.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: Purpose of Pointers?

 
0
  #4
Jan 4th, 2005
Sorry the version using new operator should look like this:

<code>

int n;
int* ptr = new int[n]; //the array ptr has interger type but size not defined

cin >> n; //ask user to enter size of array, now array has size

</code>


Sorry for making confusions
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Purpose of Pointers?

 
0
  #5
Jan 4th, 2005
First, use [ code ] [/ code ] tags (not <code>)

>>int ptr[n];
Don't declare an array with undefined dimention.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: Purpose of Pointers?

 
0
  #6
Jan 4th, 2005
Let me quote the lecture notes from my school:

  1. int main(void)
  2. {
  3. cout << “How many students? “;
  4. cin >> n;
  5.  
  6. //Declare an array to hold students' information
  7. int *grades = new int[n];
  8.  
  9. . . . . . .
  10. . . . . . .
  11. . . . . . .
  12. }
The above example they call it 'dynamic array'. The value of n is essentially unknown during compilation time. Am I right? Now look at this:
  1. int main(void)
  2. {
  3. cout << “How many students? “;
  4. cin >> n;
  5.  
  6. //Declare an array to hold students' information
  7. int grade[n];
  8.  
  9. . . . . . .
  10. . . . . . .
  11. . . . . . .
  12. }
The size of n is also not known until the programme is run. Then what is the point to use pointer and new operator to declare the array in the first case?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Purpose of Pointers?

 
0
  #7
Jan 4th, 2005
Sorry, my bad, I wasn't paying attention.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Purpose of Pointers?

 
0
  #8
Jan 4th, 2005
>Then what is the point to use pointer and new operator to declare the array in the first case?
Because the second case is ill-formed and thus, illegal in C++. Array sizes in C++ must be constant integral values. If it works when you try it then that's because your compiler allows it as an extension. Only the first case is guaranteed to be correct (assuming n is already defined).
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 150
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Purpose of Pointers?

 
0
  #9
Jan 4th, 2005
Pointers are still a vast subject that I am still working on to learn. I still need time to look over all of the technical writing of it all. I just needed to grasp the purpose of them.

but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
by: kaiser<lucy>

I can kinda see the purpose now, dealing with memory usage. Thanks :cheesy:
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Purpose of Pointers?

 
1
  #10
Jan 4th, 2005
pointers are c++ most useful things and is what sets it aside from other languages. Just to name a few uses:

Dynamic arrays
Linked lists
Memory management (eg. passing pointers rather than objects, alloc'ing memory ect..)
Binary Trees and Heaps (great for gaming)
Speed improvement (you can simplify code in some cases with pointers/function ptrs)

the last few being especially important in game programming.

If you still arent sure they are useful then try this: make a list class (or a set of functions) that can grow and shrink in size BUT not lose the contents (so it is NOT dynamic arrays...) without using STL (which, I believe, is a steam based linked list anyway). TIP: You need a pointer to the next and previous list item for it to work!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC