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, 11:22 AM   #11
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
Quote:
Originally Posted by 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.
Pointers are absolutely essential. Start using them whenever you can and become familiar with their uses; trust me, you'll thank yourself years down the road.

A good practice problem is to make a function that takes a string and parses it into an array of tokens. For example:
Quote:
char** array = split("abc 123 asdf foobar");
would return
Quote:
array[0] = "abc";
array[1] = "123";
array[2] = "asdf";
array[3] = "foobar";
array[4] = NULL;
After you finished, if you used strcpy() anywhere, rewrite the function with the only allocations being for the size of the array, and the size of the initial string (hint: each index is a pointer to somewhere in a copy of the string you save).

I feel this problem is fairly good at teaching you practical uses for pointers, and a string parser is something you may see in the field.
para is offline   Reply With Quote
Old Dec 16th, 2005, 11:31 AM   #12
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
DaWei: unfortunately i don't recall ever using a malloc, with or without a pointer, so there you go

para: i'll have a look at what you mean, thanks...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 12:46 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
DaWei: unfortunately i don't recall ever using a malloc, with or without a pointer, so there you go
Perhaps you wouldn't consider it so amusing if you realized that not using the heap, whether through malloc or new, is not a positive, but a negative, commentary on skill.
__________________
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, 12:53 PM   #14
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 am not denying the fact that i do not know as much as you, and i do realise that it is a negative comment on my skill but i am currently researching as to what a malloc is!!

EDIT: I now know what it is, but i do not believe that i have had cause to use it yet...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 16th, 2005, 7:23 PM   #15
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
You'd probably be better of sticking to new than malloc anyway.

It is true you are investigating, which is positive.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 17th, 2005, 1:23 AM   #16
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5 lectricpharaoh will become famous soon enough
Pointers are one of those concepts that can sometimes take a while to wrap your mind around. However, as others have said, knowledge of how to use them is essential in writing anything but the most trivial programs in an efficient manner. The only downside to pointers is that they have a huge potential to cause damage if they are misused, though in a secure operating system, errant pointers should no longer bring down the entire system, format your hard drive, or do other such catastrophic things as they could do under legacy operating systems like DOS.

This perceived potential for misuse coupled with the fact that learning to use them properly often takes a bit of practice are key reasons that certain languages discourage their use (C# comes to mind) or remove them entirely (Java). However, even this is an illusion. When you pass an object to a function in many of these languages, it does not pass the object itself, but a reference (read: 'pointer') to the object instead. This is because the language designers acknowledge the efficiency in doing so. However, using them in other ways is often clumsy at best. The support in C++ of low-level constructs such as pointers is a big reason that it's such a widely-used language.

I suggest you follow the link in DaWei's sig to the pointers introduction. You might need to read it more than once as you play around with pointers. Don't trust the one on (I think) cprogramming.com; if it's the one I remember, it's full of subtle errors and misinformation.

When you're done, you should be able to write a function to swap two variables, and actually have them be swapped. You should hopefully understand the relationship between a pointer and an array (the two can often be used synonymously), as well as the differences. You should learn how pointer arithmetic is performed, and that it is scaled according to the size of the pointed-at quantity (this is why pointers need a type, such as 'int' or 'char'). You should know why array[7] and 7[array] mean the same thing (hint: remember that addition is commutative).
__________________
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 17th, 2005, 4:59 AM   #17
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Thanks guys, i'm definately getting there now, i'm starting to understand, and i will read DaWei's intro...

Cheers!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 17th, 2005, 5:46 AM   #18
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by lectricpharaoh
I suggest you follow the link in DaWei's sig to the pointers introduction. You might need to read it more than once as you play around with pointers. Don't trust the one on (I think) cprogramming.com; if it's the one I remember, it's full of subtle errors and misinformation.
I think you mean the one at cplusplus.com, but cprogramming.com might also be bad? In addition to DaWei's intro to pointers, Narue also has a nice pointer tutorial on her site.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 17th, 2005, 5:49 AM   #19
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
OK i'll check out Narue's too...

EDIT: can't find it on Narue's site. Can i have a link?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 17th, 2005, 7:28 AM   #20
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Here.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion 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 11:02 PM.

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