I have just started to go through the rest of the C++ Tutorials on this website and have just done pointers. What is the point in pointers and for what might you use them. (No Pun Intended). Some example code might be useful.
Printable View
I have just started to go through the rest of the C++ Tutorials on this website and have just done pointers. What is the point in pointers and for what might you use them. (No Pun Intended). Some example code might be useful.
Try writing say strlen() without using pointers.
Ehh, I think something much more important then that where pointers are usefull, is datastructures, such as the linked list, which you might touch on after the pointers tutorial (I haven't actually read threw the tutorials here)
there should be tons of examples already, but the point of a pointer is to point to the location in memory of a variable, that way, if you pass a pointer to the variable to another function, and that function changes the value of that variable, you can see the change once the function has ended.
This code will print out:Code:
void f(int x)
{
x = 7;
}
void f2(int *x)
{
*x = 777;
}
int a = 10;
f(a);
cout << "a = " << a << endl;
f(&a);
cout << "a = " << a << endl;
a = 10
a = 777
as you notice, the first function didn't actually modify the variable because you only passed in the value of the variable and it created a new variable containing that value. In the second function, you passed in the address of your variable and it used that pointer to modify the value, so when the function returned, you can see the variable changed.
Pointers are also used to pass arrays to functions:
(You may see func() declared like this:Code:void func(int *array) {
}
int a[10];
func(a);
)Code:void func(int array[])
It's not important to know the why's at this point. For now, concentrate on learning the mechanics of how they work. Later, when you learn about functions, the why's are taught, and you'll learn the difference between "passing by value" and "passing by reference".Quote:
What is the point in pointers and for what might you use them.
However, trying to learn C++ from a bunch of one page tutorials is probably not the best way to go about it. Get a C++ book. Most C++ books are huge, so if you don't want to commit that much time and effort, you might want to try a book like "C++: A Beginner's Guide", which gives a more cursory overview of the language--and it's cheap.
If you don't want to commit that much time and effort, then get a different profession or change your major in college :D Its a little like learning to play the piano well -- can't be done without a great deal of time and practice. If you are unwilling to do that, then you might as well be playing computer games.Quote:
Originally Posted by 7stud
I am only at High School and I am learning and am willing to learn.
Well as 7stud said, don't worry about why's. You're still young and have plenty of time to gain experience. Think of your programming side as a new person born inside of you. When you're little, your parents teach you all sorts of things that you should know and do and they don't tell you why. It doesn't matter at that point, you just know that they mean something and you should know them. Eventually the time will come where you need to know why and by then you'll find that you already know it.
nice analogy!Quote:
Originally Posted by SlyMaelstrom
Isn't it...i just wish someone would analyse my File Output problem in a way that I can nderstand.