By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

Passing the value by reference is same as pointer by reference

P: n/a
I have the following programs and please clear my doubts. Passing the
value by reference is same as pointer by reference. Is this true?

What is the difference between display and display2?
How can I change display1 to value by reference if there is
difference?

void display(int &i){// Passing value by reference
++i;
}

void display2(int *i){ // Pointer by reference
++(*i);
}

void display1(char *pchr){ //I want to change to Value by reference if
any difference.
cout << pchr;
strcpy(pchr,"Hello");
}
int main(){

int i =0;
int *i1=new int;
char *str = new char [20];

display(i);
cout << i << endl;
strcpy(str,"Temp");
display1(str);
cout <<"Value="<< str << endl;

*i1=0;
display2(i1);
cout << *i1 << endl;
}
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sam pal wrote:
I have the following programs and please clear my doubts. Passing the
value by reference is same as pointer by reference. Is this true?

What is the difference between display and display2?
How can I change display1 to value by reference if there is
difference?

void display(int &i){// Passing value by reference
++i;
}

void display2(int *i){ // Pointer by reference
++(*i);
}
They will have the same effect, iff the argument `i' in display2 points
to a valid int. The advantage with references is that you don't have to
worry about a NULL pointer.

void display1(char *pchr){ //I want to change to Value by reference if
any difference.
cout << pchr;
strcpy(pchr,"Hello");
}
Here, you're changing what pchr points to, as opposed to pchr itself.
int main(){

int i =0;
int *i1=new int;
char *str = new char [20];

display(i);
cout << i << endl;
strcpy(str,"Temp");
display1(str);
cout <<"Value="<< str << endl;

*i1=0;
display2(i1);
cout << *i1 << endl;
}


HTH,
--ag

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2

P: n/a

"sam pal" <sa*****@hotmail.com> wrote in message news:f4**************************@posting.google.c om...
I have the following programs and please clear my doubts. Passing the
value by reference is same as pointer by reference. Is this true?
No. And that's not what you are doing.
What is the difference between display and display2?
How can I change display1 to value by reference if there is
difference?


display2 doesn't pass the pointer by reference. It passes a pointer
by value.

display1 and display2 are rougly equivelent, other than the vehicle
for refering to the original object is a pointer in one and a reference
in the other.

The practical differences are what you have observed.
Jul 19 '05 #3

P: n/a
sam pal wrote:
I have the following programs and please clear my doubts.
Passing the value by reference is same as pointer by reference.
Is this true? What is the difference between display and display2?
How can I change display1 to value by reference
if there is difference?
void display0(int i){ // Pass by value
++i; }

void display(int& i){ // Pass by reference
++i;
}

void display2(int* p){ // Pass a reference *through* a pointer
++(*p); // (pointer p is passed by value)
}

void display1(char* p){
//I want to change to Value by reference if any difference.
std::cout << p << std::endl;
strcpy(p, "Hello");
}
void display3(char& c){ // Pass by reference
std::cout << &c << std::endl;
strcpy(&c, "Hello");
}
int main(int argc, char* argv[]){

int i = 0;
int* i1 = new int;
char* str = new char[20];

display(i);
std::cout << i << std::endl;
strcpy(str, "Temp");
display1(str);
std::cout <<"Value="<< str << std::endl;

*i1 = 0;
display2(i1);
cout << *i1 << endl;
}

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.