Pass by pointer
I just read the FAQ regarding parameter passing techniques....it says if we pass by pointer(address) then changes in address of the pointer in the function is not reflected in the calling function...i tested it and it worked as it says......
But we can pass like this..so that pointer address can't be changed....it works fine
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
void f(int*);
int main()
{
int x=10;
cout<<x<<endl<<&x<<endl;
f(&x);
cout<<x<<endl;
return 0;
}
void f(int *const p)
{
cout<<p<<endl<<*p<<endl;
}
Plz comment
# 1 Re: Pass by pointer
I'm not at all sure I know what you're asking, but I think this is it. Take a look at this program:
#include <iostream>
using namespace std;
void f(int* p)
{
int x = 5;
p = &x; // OK: Assigning p a new address is legal, but the
// change only applies within f().
cout << *p << '\n'; // OUTPUT: 5
}
void g(int* const p)
{
int x = 10;
p = &x; // ERROR: l-value specifies const object
*p = 5; // OK: modifying the value p points to is legal
}
int main()
{
int x = 15;
cout << x << '\n'; // OUTPUT: 15
f(&x);
cout << x << '\n'; // OUTPUT: 15 - x is unchanged after calling f().
return 0;
}
Are you asking about the difference between the properties of p in f() and g()? If so, the difference is that in f(), the value of p can be changed. Still, p is a local variable, so any changes you make to that address are only effective within the body of that function. Changing the value of p doesn't change the address of x, as you can see by the fact that the program's main() function prints the same result before and after calling f().
With something like g(), on the other hand, p cannot be changed at all; that's the effect of the const keyword in the argument list. This program won't compile as is, because g() attempts to change the value of a const variable. Note that you can still change the value that p points to, though. An int * const is a constant pointer to a changeable int. A const int * is a changeable pointer to a constant int. And a const int * const is a constant pointer to a constant int. Hopefully this makes sense.
# 2 Re: Pass by pointer
I know this...what i am saying is...if we pass by reference...i guess we can not change the address of the reference variable..isn't it(correct me if i am wrong)...but when we pass pointer by value we can change the address and that change is not reflected in calling function...so for that i am passing as constant pointer...so that address can't be changed in the called function and problem with passsing by pointer is removed...
So what i was asking was does passing by const pointer removes the problem?
# 3 Re: Pass by pointer
I'm sorry, but I don't understand what you're saying. If you understand the difference between passing a pointer and passing a const pointer to a function, which it sounds like you do, then what is your question? The fact that you can change the value of a pointer (one that isn't const, anyway) isn't a "problem" in all circumstances. If you don't want the called function to be able to change the value of a pointer, you make it const. Otherwise, you don't. But as I said, it sounds like you've already figured that out, so what are you asking?
# 4 Re: Pass by pointer
Let me put it in totally different way
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....this is what i am asking.....
plz comment
# 5 Re: Pass by pointer
I know this...what i am saying is...if we pass by reference...i guess we can not change the address of the reference variable..isn't it(correct me if i am wrong)...but when we pass pointer by value we can change the address and that change is not reflected in calling function...so for that i am passing as constant pointer...so that address can't be changed in the called function and problem with passsing by pointer is removed...
So what i was asking was does passing by const pointer removes the problem?
things to be remember we are not passing the value in function by pointer that it will remain same .
if we want to take return value then we pass it by pointer .if we send simple by call by value and change it in function .it will reflect only in functionand only local copy effect take place but if we want to take outvalue from function so we pass it by refrence or pointer then only we can achieve
you are making it const so remember you can't modify the value of const .so here it's doesn't surve any work .if you simply pass by value then also it will work.in case of const you can't change the value .and value will remin.in your question you are also using a poinyter to constant
and with the help of a pointer you can change the value of a constant also
both code is will give you proper output .const int* u;
Starting from the identifier, we read u is a pointer, which points to a const int. Here, no initialization is required because youre saying that u can point to anything (that is, it is not const), but the thing it points to cannot be changed.
Heres the mildly confusing part. You might think that to make the pointer itself unchangeable, that is, to prevent any change to the address contained inside u, you would simply move the const to the other side of the int like this:
int const* v;
Its not all that crazy to think that this should read v is a const pointer to an int. However, the way it actually reads is v is an ordinary pointer to an int that happens to be const. That is, the const has bound itself to the int again, and the effect is the same as the previous definition
void f(int *const p)
{
*p=12;
}
void f(int *p)
{
*p=7;
}
# 6 Re: Pass by pointer
Why in C++ passing a pointer is not termed as a parameter passing technique
It is a parameter-passing mechanism, usually termed "passing by address." It's not flawed per se. If you want to pass a pointer and don't want its value altered within the called function, then you can use const, but if you do so, you're addressing the requirements of your specific program, not fixing a "problem" that exists with passing by address in general. There are times when a programmer might want a function to be able to alter the value of a pointer that it receives as an argument.
# 7 Re: Pass by pointer
It is a parameter-passing mechanism, usually termed "passing by address.
Plz see this
http://www.dev-archive.com/forum/showthread.php?t=343473
# 8 Re: Pass by pointer
Well, let me put it this way. Supposing you have some object x to pass to a function, most people would consider three alternatives: pass the object itself, pass its address (in the form of a pointer), or pass a reference. In that sense, you could say that there are three ways to make x available to whatever function it is that you're calling. The FAQ entry you linked to is correct in that the act of passing a pointer to x in fact uses pass-by-value semantics, just with the address of x rather than x itself. But that's just how pointers work; the FAQ doesn't dismiss passing by address as a parameter-passing mechanism because there's something wrong with it. There's nothing wrong with it. The author is saying that pointer-passing is sometimes classified incorrectly, not that it's flawed and needs to be fixed somehow.
That said, while I agree with the FAQ, and I'm sure the author (cilu) is a more knowledgeable programmer than I am, perhaps "misconception" is too strong a word there. If you think that pointers exist only to allow another means of passing values to a function, then you are indeed laboring under a misconception: you're seeing pointers too narrowly, and not really understanding their full usefulness. But I wouldn't say that the idea of having three basic options for making an object available to a function is wrong; the concept of passing by address is a useful one, even if, strictly speaking, it's fundamentally the same thing as passing by value.
# 9 Re: Pass by pointer
See if you are trying that hard. Remember that there are just 2 ways of passing parameters to a function. One by value and the other by reference. When we pass the address of a variable in a function using the pointer mechanism that you already pointed out then actually that pointer is being passed by value. So, the thing that you understanding to be passed by pointer or address is actually passing the pointer by value. But yes, we could consider an altogether different mechanism because the things being passed by value are different. Once the object itself is passed by value calling the copy constructor of the object and once the pointer is being passed by value in which case a copy of the pointer would be created, usually in the old C- fashion - bitwise copy. The two things seem to be quite different isn't it? I dont what the standard has to say about this but probably it would be defining just the pass by value and the pass by reference mechanism. (I tried looking out but couldn't get any mention on that and if that is the case then I think you could customize the concept as per your understanding and perception.)
I think you do understand what const argument and ref arguments mean (looking at your posts). Regards.
# 10 Re: Pass by pointer
in my opinion, there should also be some guidelines to when using "pass by reference", "pass by const reference" etc.
Below you can read some of the guidelines I am using:
1. pass by value:
function doesn't modify the outside object and
the object is small (either fundamental type - int, double, etc - or some small class, struct - because a copy of that object is made) and
the object has a good user defined copy-constructor
2. pass by const reference:
function doesn't modify the outside object and
the object is a big one or the copy constructor is not going to do the right job.
3. pass by reference:
function call looks like it takes the value of the object, but it can modify it (usefull in operator overloading). user doesn't worry about the function modifying his object (or such behaviour is obvious)
4. pass "by pointer":
user has to be aware that the object will/may be modified inside the function.
also, a very interesting and sometimes hard to understand problem, wich comes into discussion especially when overloading operators, is the way of returning from a function: return by value, by reference or by const reference?
a_c at 2007-11-9 0:59:05 >

# 11 Re: Pass by pointer
You could also write something like this:
void TakesAPointer(int* p)
{
int i = 5;
*p = i;
cout << *p << endl;
}
Which will change the value pointed to by p even after the function call. If you don't want the value of p or *p to change then you will need to change your function to:
void TakesAPointer(int const * const p)
which for the above code will give you a compile time error in VS6 of
error C2166: l-value specifies const object
# 12 Re: Pass by pointer
If you don't want the value of p or *p to change then you will need to change your function to:
void TakesAPointer(int const * const p)
why not simply passing a const reference? in my opinion, the code would much clearer then.
a_c at 2007-11-9 1:01:10 >

# 13 Re: Pass by pointer
why not simply passing a const reference? in my opinion, the code would much clearer then.
I agree. I meant to say that you could do if you wanted to.