What is the point of pointers? Allright, you can store values in RAM with it. So can you with:
So what's the point of using pointers?Code:int a = 3;
What is the point of pointers? Allright, you can store values in RAM with it. So can you with:
So what's the point of using pointers?Code:int a = 3;
I suggest that you read the tutorial.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Allright maybe I should have stated my question more clearly. I've read the tutorial, a couple of times. I've used pointers, but still haven't found an advantage in them above the normal integers I use for my (simple) programs.
The only really useful point I see is that you can get an amount of memory for a certain junk of data of which you don't know the size beforehand another is: (I cite) "If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data!" I don't really understand this since I just use the variable. I bet all of this is really useful if you're good at C, but it's a beginner's tutorial so what's the use of it for me?
Last edited by omnificient; 12-14-2007 at 11:22 AM.
Try implementing something trivial like strcpy() without using pointers.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Doing like you do stores memory on the stack which is limited, usually to around 1 MB (and even less in embedded systems!).
Also, all variables allocated on the stack disappears when the function exits!
Further, if you pass your data to other functions without pointers, a copy is created! So the function you call will be able to modify its own copy of the data, thus the changes are not reflected in the function (caller) that called the new function (callee).
If you want the callee to be able to manipulate or change the memory you passed, you need to do so using pointers.
No, you're right, pointers are completely useless - no need to learn about them....
Of course, if that was the case, why the the jokers that invented the language include them, and why are people using them all the time?
Perhaps they are useful, after all?
Actually, I can make strcpy() without (apparent) pointers;
--Code:void strcpy(char dest[], char src[]) { int i; for(i = 0; dest[i] = src[i]; i++) ; }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
But that is basically pointers, is it not? Though you may say the function takes arrays, in reality the compiler will treat them as pointers and pass the address. And the values also inhibit the properties of pointers if you try to look at their contents (you can only see the first index, though the caller can see the entire).
Ah thanks, now it's clear!
It's like the difference between shipping a house to someone so that they can see it, or sending them the house address so they can go see it where it's at. Which is more efficient?
Yes, but of course, since you ship a house, it's not THE house - it's just similar so you can change it but the real house won't see a difference.
Of course, this applies to really big objects. If it's just a lamp, the difference is less subtle, but there.
Just stick something like "why use pointers" into google . . . .
http://duramecho.com/ComputerInforma...CPointers.html
http://www.sparknotes.com/cs/pointer...s/summary.html
http://www.cs.cf.ac.uk/Dave/C/node10.html
Without pointers, you can't use dynamic memory allocation. Enough said.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Not a resurrection of the "pointless" thread . . . you know, the one that got locked because everyone was cracking point jokes?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.