|
-
Oct 4th, 2002, 08:15 PM
#1
Thread Starter
Stuck in the 80s
Pointers
Where can I find a good site about pointers? Documentation, tutorials, explainations, etc?
-
Oct 4th, 2002, 10:34 PM
#2
Thread Starter
Stuck in the 80s
Okay. I have a basic understanding of what they do.
I'm just confused as to why?
Can anyone give me a practical example of using pointers?
-
Oct 5th, 2002, 04:57 AM
#3
Monday Morning Lunatic
Do a search round the forum, I explained this in detail a few months back 
Here's one use:
Code:
bool get_number(int key, int *target) {
if(key > 20)
return false;
*target = raw_lookup(key);
return true;
}
...or something. Bit contrived, but it's a way of returning more than one value from a function. They have far more uses though...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 5th, 2002, 11:18 AM
#4
Junior Member
As far as i understand them, they let you play with variables that dont' belong to the function you're in.
But i'm a complete newb, so take what i say with a grain of salt.
-
Oct 5th, 2002, 12:23 PM
#5
Thread Starter
Stuck in the 80s
What's the difference between that and references then? like:
Code:
void myFunction(int &bing)
-
Oct 5th, 2002, 03:01 PM
#6
In this case references are C++'s answer to the problem pointers caused: bad pointers, ugly and hard-to-read syntax.
references don't exist in C, so in C++ always use references unless you have a damn good reason to use pointers.
There are many other uses to pointers, the most common are traversing arrays (including char arrays) and dynamic memory allocation.
And a reference cannot be set to NULL, which sometimes can cause problems.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 5th, 2002, 10:47 PM
#7
Thread Starter
Stuck in the 80s
hmm...
I don't know why I find them so confusing, but I still don't have a damn clue.
I'll just have to wait until they're covered in one of my classes.
Thanks for your help!
-
Oct 6th, 2002, 02:24 AM
#8
Hyperactive Member
-
Oct 6th, 2002, 09:16 AM
#9
Frenzied Member
suppose you want the length of a null-terminated char array:
This says: step thru each character, keep going as long as the char is not a zero. increment a counter for each non-zero char.
Code:
for(char *buf=somestring,int len=0;*buf;len++,buf++);
At loop exit, len equals the number of characters.
This is essentially what strlen() does.
-
Oct 6th, 2002, 01:22 PM
#10
Except that strlen will not increment a counter, but rather use pointer arithmetic:
Code:
size_t __cdecl strlen(const char* str) {
const char *run;
for(run=str;*run;++run);
return run - str;
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|