User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 361,565 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,046 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4095 | Replies: 19 | Solved
Reply
Join Date: Dec 2004
Location: Hiawassee, Georgia
Posts: 129
Reputation: Geek-Master is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Purpose of Pointers?

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Posts: 13
Reputation: kaiser<lucy> is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
kaiser<lucy> kaiser<lucy> is offline Offline
Newbie Poster

Re: Purpose of Pointers?

  #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  
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: Purpose of Pointers?

  #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  
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Help Re: Purpose of Pointers?

  #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  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Purpose of Pointers?

  #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  
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: Purpose of Pointers?

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

int main(void)
{
     cout <<  “How many students? “;
     cin   >> n;

     //Declare an array to hold students' information
     int *grades = new int[n];

    . . . . . .
    . . . . . .
    . . . . . .
 }
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:
int main(void)
{
     cout <<  “How many students? “;
     cin   >> n;

     //Declare an array to hold students' information
     int grade[n];

    . . . . . .
    . . . . . .
    . . . . . .
 }
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  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Purpose of Pointers?

  #7  
Jan 4th, 2005
Sorry, my bad, I wasn't paying attention.
Reply With Quote  
Join Date: Sep 2004
Posts: 5,838
Reputation: Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold 
Rep Power: 24
Solved Threads: 384
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Purpose of Pointers?

  #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).
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Dec 2004
Location: Hiawassee, Georgia
Posts: 129
Reputation: Geek-Master is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Purpose of Pointers?

  #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  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Purpose of Pointers?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC