Pointers
Hey,
I am fairly new to programming and the only thing that is really holding me up is pointers. I have read about them and i understand the syntax and how to use them. The only problem is that i just dont get when they should be used.
Can anyone help me understand WHEN they should be used? It would really help me a lot if anyone could do that. Or could anyone give examples of certain situations of when they should be used?
Thanks to anyone who can help.
At first I also didn''t see the point in using them.
However, when you start to use classes, they become really usefull.
- When you pass a pointer to a function instead of a variabel, you can actually change the variable it points to, so you don''t have to return your value to the calling function....
- When you work with multiple classes, you can access a function in any class by getting it''s pointer. This way, you don''t have to make everything Global.
- When you work with ''arrays'' that have to be of variabel size, pointers are the only way to do it.
However, when you start to use classes, they become really usefull.
- When you pass a pointer to a function instead of a variabel, you can actually change the variable it points to, so you don''t have to return your value to the calling function....
- When you work with multiple classes, you can access a function in any class by getting it''s pointer. This way, you don''t have to make everything Global.
- When you work with ''arrays'' that have to be of variabel size, pointers are the only way to do it.
There useful when your using linked list cause you have to call other classes.
Stick
Stick
struct Node
{
Node* left;
Node* right;
//data goes here
};
{
Node* left;
Node* right;
//data goes here
};
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
when you want to know why you use them, you can declare structs or classes that arent filled yet or you are declareing:
struct cheese{
cheese* milk;
};
if you put cheese milk in it it wont compile right.
struct cheese{
cheese* milk;
};
if you put cheese milk in it it wont compile right.
quote: Original post by Ronin_54
- When you pass a pointer to a function instead of a variabel, you can actually change the variable it points to, so you don''t have to return your value to the calling function....
All of the other oints so far have been good ones
For this one example, though, I would recommend using references instead.
Turring Machines are better than C++ any day ^_~
quote: Original post by intrest86
For this one example, though, I would recommend using references instead.
*snickers*
I usually use pointers for it.
Initially, it is hard to understand the use of pointers. I assume that you are using C++ rather than C, so you have passed by reference using C++''s ''&'' symbol in your headers, thus, have not really passed pointers around.
But when you start to deal with dynamic memory (allocating memory at run-time using malloc() or in C++, ''new'') you will be able to understand pointers properly. An example is that you could declare a pointer, then use it to create an array.
Eg. int *x;
int count;
scanf("%d", &count); // OR cin>>count;
x=new int[count]; // OR x=(int)malloc(count*sizeof(int));
This will create an array of a size sepcified by the user at run-time.
Hope this helped a little. Once you fully understand them, you will know when to use them.
But when you start to deal with dynamic memory (allocating memory at run-time using malloc() or in C++, ''new'') you will be able to understand pointers properly. An example is that you could declare a pointer, then use it to create an array.
Eg. int *x;
int count;
scanf("%d", &count); // OR cin>>count;
x=new int[count]; // OR x=(int)malloc(count*sizeof(int));
This will create an array of a size sepcified by the user at run-time.
Hope this helped a little. Once you fully understand them, you will know when to use them.
This thread helped me. I am trying hard to get a firm grasp on Pointers and arrays and then i am moving slowly into an API.
This may be a mistake but i just want to learn a little about 3d.
This may be a mistake but i just want to learn a little about 3d.
September 28, 2002 10:37 PM
you can''t really be told... one day you''ll be in a situation... wondering how to do something... then it will hit you. Thats the best way to know when to use them. For now knowing how is all that matters.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement