-
Passing pointers
Why in C++ passing a pointer is not termed as a parameter passing technique.....Is it because it has a flaw of some kind i.e. address is not reflected back/we can change the address in called function..is that so...if that's the reason....then we can pass it as const pointer as a remedy.....
Plz comment
-
I'm confused as to what you're asking. Post some code and/or clarify your question.
Passing pointers to (I assume) functions is valid C++. Take the following:
Code:
#include <iostream>
void inctwice(int *y)
{
*y += 2;
}
int main()
{
int x = 2;
inctwice(&x);
std::cout << "Value of x: " << x << std::endl;
}
That will say that x is four at the end of the program. The same could be accomplished with references as well.
-
What i mean is why is it not considered as a parameter passing technique in C++
-
It is considered as a parameter passing technique in C++.
References are often preferred because you don't need to check for null (since pointers can legally be null), and because the syntax of a reference is more natural.
-
Maybe for most purposes references provide a safer and cleaner way of achieving the same effect as pointers.