Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 8:19 AM   #1
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Pointers! Why?

I have just about grasped the idea of pointers, but i am still confused as to why you would need them.

This code is taken from C++ Without Fear by Brian Overland

Consider this:
#include <iostream>
using namespace std;

void double_it(int *p);

int main()
{
       int a = 5, b = 6;
       cout << value of 'a' before doubling it: " << a << endl;
       cout << value of 'b' before doubling it: " << b << endl;
       double_it(&a);
       double_it(&b);
       cout << value of 'a' after doubling it: " << a << endl;
       cout << value of 'b' after doubling it: " << b << endl;      
       return 0;
}

void double_it(int *p)
{
       *p = *p * 2;
}

How is this anymore useful/effective/amicable than the following?

#include <iostream>
using namespace std;

int double_it(int p);

int main()
{
       int a = 5, b = 6;
       cout << value of 'a' before doubling it: " << a << endl;
       cout << value of 'b' before doubling it: " << b << endl;
       a = double_it(a);
       b = double_it(b);
       cout << value of 'a' after doubling it: " << a << endl;
       cout << value of 'b' after doubling it: " << b << endl;      
       return 0;
}

int double_it(int p)
{
       p = p * 2;
       return p;
}
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 9:25 AM   #2
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
The differences, the one with pointer allow you to modified the variables passed to the function. they are passed by ref meaning when the pointer variable is changed. the variables passed to the function is also changed. the other code you posted, simply pass by val, meaning it will be only be changed when you use '=' to the function(var). The use of pointer in my opinion makes it easier to modify a variable by using the address. I hope it is the right answer.
Master is offline   Reply With Quote
Old Dec 16th, 2005, 9:30 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
The first one modifies the variables directly, the second makes a copy of the variable passed. This might not be a big deal in the case above but when working with huge objects, making a copy will make your program memory inefficient.
OpenLoop is offline   Reply With Quote
Old Dec 16th, 2005, 9:31 AM   #4
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
I understand what it does, but why would you want to use pointers? As far as i can see it doesn't make it any easier or anything, just a different way of doing things...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 9:36 AM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5 lectricpharaoh will become famous soon enough
There are a lot of reasons. Bear in mind that in C++ (not C), you can use reference types as an alternative to passing pointers. That said, the two are the same principle; using reference variables in C++ is essentially a shortcut for passing pointers, letting the compiler worry about generating the appropriate indirection, rather than having to do it by hand.

A few examples of where you might want to pass a pointer (or reference) include:

What you are passing is large, such as a struct or class. Data in C/C++ is passed to functions on the stack (barring certain compiler-specific extensions), and this is not an efficient way to pass large items.

It allows you to modify the original variable, rather than a local copy. You point out in your example that you could just return a value, and while this is true in your example, in many, it is not. Imagine you wished to modify several values; assigning through return only lets you modify one.

If what you are dealing with is an allocated block of memory (such as an array), it makes sense to pass a pointer to that block (or a subsection of it). In fact, short of passing every single memory location as a separate parameter, passing a pointer is the only way.

If you have a function that swaps two items, and you want them to actually be swapped in the caller, not just the callee, using pointers is the way to do this. This approach is used heavily in sorting. In fact, often you might swap pointers to the data objects themselves, meaning you're not merely dealing with pointers, but instead pointers to pointers. This allows you to do a sort or similar operation as efficiently as possible (assumign your algorithm is likewise efficient), because it's much less work to swap a pair of pointers (generally four bytes on modern systems) than objects which may be dozens or hundreds of bytes- maybe even more.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 16th, 2005, 9:39 AM   #6
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
AH ha i see! Thanks lectricpharaoh, that's cleared that up. I can definately see where you're coming from.

Cheers!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 9:47 AM   #7
Opcode
Newbie
 
Join Date: Dec 2005
Posts: 1
Rep Power: 0 Opcode is an unknown quantity at this point
Quote:
Originally Posted by LOI Kratong
I understand what it does, but why would you want to use pointers? As far as i can see it doesn't make it any easier or anything, just a different way of doing things...
I will try to explain the point of pointers with a concrete practical explanation.

Let us say we have an array of numbers for examples sake we have an array of ten thousand numbers.

Now if we are using an array and we want to delete the first element we would have to shift the array 9999 elements. This would require a relatively long time since we have a lot of operations to perform. Are you following this logic to date?

If we are using pointers we can create a type of list called a linked list. A linked list is a list of elements that uses pointers to reference the next element.

For example one element of the array would contain a value and a pointer to the next element. Now back to the case of the ten thousand numbers. If we are using a linked list and we want to delete the first element we simply change where the pointer points and voila we are now done. The difference in efficiency is staggering is it not? One operation versus 9999 operations for the array.

If your still confused look up a tutorial that explains linked lists in further detail.
Opcode is offline   Reply With Quote
Old Dec 16th, 2005, 9:51 AM   #8
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Yeah i kinda get it... when i've thought about for a while i'll get it solidly
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 10:46 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Have you ever tried to use "malloc" without a pointer?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 10:50 AM   #10
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
Pretty much the only time pointers are used in my programs, as far as I can think of, is in constructors and destructors for classes.
Riddle is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC