I can't say I'm a newbie, but I can't say I'm a complete n00b either .
My question is this:
Why would you need pointers? I know how they work, and I know how to use them. What I don't know is why should I use them.
Just curious.
I can't say I'm a newbie, but I can't say I'm a complete n00b either .
My question is this:
Why would you need pointers? I know how they work, and I know how to use them. What I don't know is why should I use them.
Just curious.
Pointers are great if you want to create an array with a number of elements that is not constant (if the user enters a number, an array with so many elements will be created). You can't do something like this:
int Array[NrOfElements];
Do this instead:
int* Array;
Array=new int[NrOfElements];
delete[] Array;
Pointers have lots of other uses too, this is only one of them.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
I didn't know about that . Any more ways to use pointers?
Put simply and without having to describe every situation, pointers give C and C++ their power. Using pointers you can write code that is much more efficient in both speed and memory usage, easier to use in the case of abstract data types such as trees and linked lists and more compact in relation to similar code in other languages.
-Prelude
My best code is written with the delete key.
I'll pretend I understood that. I will have to, some day. Not any time soon, though.
All in good time, you'll begin to understand the more you use them. For now I'm afraid it's just going to be one of those things that you have to do now and trust that you'll learn later
-Prelude
My best code is written with the delete key.
Go back and take another look at the array reply and its use of new. New returns a pointer, so that's a good reason for needing to know how to deal with them.
So why do you need New. Because C/C++ does not let you specify at RUNTIME how big an array you want, unless you use use "new". Try it: the compiler will say that size has to be a constant which means you have to enter it at compile time, not runtime.
OK you say, I don't need runtime declared arrays. Well actually, all significant memory structures you want to declare at runtime require new. It's called dynamic memory allocation.
BTW, I'm new at this, so if some guru wants to correct or clarify, I'll be happy to learn from that too.
Regards, Al
So far so good Al.
-Prelude
My best code is written with the delete key.