#include <iostream>
//using namespace std;
int * j;
void f(int * v) {
j=v;
}
int main () {
int i=111, tmp;
f(&i);
*j=222;
cout << i << endl;
cin >> tmp;
return 0;
}
output: 222
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
//using namespace std;
int * j;
void f(int& v) {
j=&v;
}
int main () {
int i=111, tmp;
f(i);
*j=222;
cout << i << endl;
cin >> tmp;
return 0;
}
output: 222
are both of these ways correct? which one is better/standard?
note: I commented out 'using namespace std' since turbo c++ on windows was generating error otherwise.
I'm not sure if the C++ standard says anything on this matter.
Are you trying to take the address of the actual parameter passed to the function?
If so, why not just pass the parameter as a pointer? Are you trying to take the
address of the formal parameter? If so, your code is essentially writing 222 to
unallocated stack space (bad).
Yes, there's no point in calling a function to obtain the address of a variable. If you take a look at your code, you'll see that you're using the necessary operator in different places.
The reference operator (&) gives the memory address of the expression to its right.
I saw some graphic library interface somewhere that user must pass some argument variables by reference (via & operator before var name) to that library's functions/class constructors.
so I thought what is the reason to bother the user to type &s each time (and possibility of omitting them), where c++ have a special notation (TYPE &par) for declaring functions that manipulate arg variables' contents directly.
the only trait that &par notation might have not was what I tested for. and results of test show the same trait as passing args as pointers/address via & operator (and of course the func's parameters itself must be declared classically (the only syntax/way in C): Type *par) by the user.
-------------
in my example since 'i' is not a func's local variable (parameters are local variables to functions, you know) and our program output is the contents extracted directly from i and not possibly the func's parameter variable's address space that is sure that what is in j is exactly the address of i and not any other thing.
but know, finally, I think that the classic and probably the only standard way for getting address of args is very better and more intuitive for several reasons (I think there is no need for explaining those reasons).
anyway that was a test that has its worth and some resulting useful insight. dont you think?
The reason for passing by reference rather than pointer is that references do not have to be checked for NULL prior to accessing whereas pointers do, and the extra pointer checks in a graphics library costs speed.
it was a graphic library (indeed a GUI lib; sorry I forgot to say exactly) written in c++; actualy they (interface functions) were things such as c++ class constructors.
now I think I found the real/main reason.
those parameters had default values! (example: void f(TYPE * p=0))
and I think it is impossible with the 'TYPE &p' declaration. is it correct?
sorry I have not access to a c++ compiler now.