Where can I find a good site about pointers? Documentation, tutorials, explainations, etc?
Printable View
Where can I find a good site about pointers? Documentation, tutorials, explainations, etc?
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?
Do a search round the forum, I explained this in detail a few months back :)
Here's one use:...or something. Bit contrived, but it's a way of returning more than one value from a function. They have far more uses though...Code:bool get_number(int key, int *target) {
if(key > 20)
return false;
*target = raw_lookup(key);
return true;
}
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.
What's the difference between that and references then? like:
:confused:Code:void myFunction(int &bing)
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.
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!
Here's a link
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
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.
At loop exit, len equals the number of characters.Code:for(char *buf=somestring,int len=0;*buf;len++,buf++);
This is essentially what strlen() does.
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;
}