943,516 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 9905
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 3rd, 2005
0

Purpose of Pointers?

Expand Post »
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:
Similar Threads
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 3rd, 2005
0

Re: Purpose of Pointers?

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kaiser<lucy> is offline Offline
13 posts
since Nov 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jimFan is offline Offline
22 posts
since Dec 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jimFan is offline Offline
22 posts
since Dec 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

First, use [ code ] [/ code ] tags (not <code>)

>>int ptr[n];
Don't declare an array with undefined dimention.
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

Let me quote the lecture notes from my school:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jimFan is offline Offline
22 posts
since Dec 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

Sorry, my bad, I wasn't paying attention.
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

>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).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 4th, 2005
0

Re: Purpose of Pointers?

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.

Quote ...
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:
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 4th, 2005
1

Re: Purpose of Pointers?

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!
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Guys! Help me please! I need you help!
Next Thread in C++ Forum Timeline: Access elements from a class in another file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC