Passing the value by reference is same as pointer by reference

S

sam pal

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;
}
 
A

Artie Gold

sam said:
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
 
R

Ron Natalie

sam pal said:
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.
 
E

E. Robert Tisdale

sam said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top