Pointers
by Joe Spataro · in Technical Issues · 03/15/2004 (2:56 pm) · 5 replies
If I have a function that takes a pointer to an int as an argument it would look like so
void MyFunc(int *i);
so when I wanted to use it, I would do something like this:
int num;
MyFunc(&num);
but, if num was already a pointer, like this
int *num = new int;
then, when I used it in the function, I dont need the &, like this:
MyFunc(num);
is this correct?
void MyFunc(int *i);
so when I wanted to use it, I would do something like this:
int num;
MyFunc(&num);
but, if num was already a pointer, like this
int *num = new int;
then, when I used it in the function, I dont need the &, like this:
MyFunc(num);
is this correct?
#2
Just thought you might like to know that there's also such a thing as implicit pointers (don't know if that's what they're actually called, but it's how I think of them ...). Try this:
// In the function definition, put an & after the variable type
void MyFunc(int& i)
{
// Inside, just use i as a normal int. For example:
i = 5;
}
// To call it, again just use the int as normal. For example:
int x;
x = 7;
MyFunc(x);
// x is now equal to 5
You can do it either way, it's really just personal preference. If you're not used to pointers though, you might find this a little easier.
03/15/2004 (7:26 pm)
This might be just confusing the issue for you - if so, feel free to ignore me! ;-)Just thought you might like to know that there's also such a thing as implicit pointers (don't know if that's what they're actually called, but it's how I think of them ...). Try this:
// In the function definition, put an & after the variable type
void MyFunc(int& i)
{
// Inside, just use i as a normal int. For example:
i = 5;
}
// To call it, again just use the int as normal. For example:
int x;
x = 7;
MyFunc(x);
// x is now equal to 5
You can do it either way, it's really just personal preference. If you're not used to pointers though, you might find this a little easier.
#3
On a side note, the above would be considered bad programming practice because you are modifying a variable passed to a function. That's what returns are for, though it has its uses especially when you want to return more than 1 value. In that case however, its usually considered "proper" to use pointers instead of references because it makes it more clear whats going on.
Note I am just commenting on what is considered "proper" programming practices, and that is no way a dig against Glen :) It was only an illustrative example anyway.
03/15/2004 (7:39 pm)
That example is whats called "referencing". MyFunc takes a reference to an int. Essentially in this case what the compiler does is convert it to a pointer and back again for you. It is a very nice way of doing things in C++ and is the "preferred" way taught by most colleges, though as with anything programming related you will find people on both sides of the argument :)On a side note, the above would be considered bad programming practice because you are modifying a variable passed to a function. That's what returns are for, though it has its uses especially when you want to return more than 1 value. In that case however, its usually considered "proper" to use pointers instead of references because it makes it more clear whats going on.
Note I am just commenting on what is considered "proper" programming practices, and that is no way a dig against Glen :) It was only an illustrative example anyway.
#4
This page is the best explanation I could google, searching for 'C++ references' isn't the best of ideas :p
www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
Edit: gah, you beat me to it John :)
03/15/2004 (7:48 pm)
What Glen describes are references. Generally, they are safer to use then pointers, as you can't do any pointer arithmetic on them. However, they are only available in C++, not C.This page is the best explanation I could google, searching for 'C++ references' isn't the best of ideas :p
www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
Edit: gah, you beat me to it John :)
#5
In any case, I'm more of the belief that you should use what makes sense. If it's obvious the function is going to be changing the value, I see no reason to complicate the issue just to conform to some standard. On the other hand, you can't go changing the value simply because it's convenient. If it's not an obvious result of calling that function, it's a pretty good bet it'll mess you (or someone else) up someday!
03/16/2004 (9:10 am)
Thanks for the clarification ... I was taught C in school and just picked up C++ on my own. So I don't have all the right terminology or practices ...In any case, I'm more of the belief that you should use what makes sense. If it's obvious the function is going to be changing the value, I see no reason to complicate the issue just to conform to some standard. On the other hand, you can't go changing the value simply because it's convenient. If it's not an obvious result of calling that function, it's a pretty good bet it'll mess you (or someone else) up someday!
Torque Owner John Vanderbeck
VanderGames