char*& or char*

P

pembed2003

Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}

void f2(char* s){
*s = 'b';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same thing?
2. When do you use the first form and the second form?

Thanks!
 
T

Thomas Stegen

pembed2003 wrote:

I have modified your program, spot the difference in the output.

void f1(char* &s){
s = "abcd";
}

void f2(char* s){
s = "efgh";
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}
 
I

Ioannis Vranos

pembed2003 said:
Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}

void f2(char* s){
*s = 'b';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same thing?
2. When do you use the first form and the second form?


With f1() you change the value of the pointed object using the original
pointer variable itself, while in the second a temporary local pointer
variable is created with the name s and the same value with the original
pointer variable.






Ioannis Vranos
 
R

Rolf Magnus

pembed2003 said:
Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}

void f2(char* s){
*s = 'b';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same
thing?

The first passes a reference to the pointer to the function, the other
one a copy of it. So with the first one, you can change the value of
the original pointer (which your example doesn't do), with the second
not, because the function is working on a local copy of it.
2. When do you use the first form and the second form?

You use the first form if you want to modify the object from the
function. And you use const references if you don't want to modify the
object, but want to avoid the overhead of copying it. The second form
is mostly used with built-int types (for which copying doesn't impose
an overhead) if you don't want to modify the original object. Also, the
second form allows to pass temporaries:

void foo(int i);
void bar(int& j);

int main()
{
foo(3); //ok
bar(3); //not possible
}
 
J

John Harrison

pembed2003 said:
Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}

void f2(char* s){
*s = 'b';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same thing?

They both do the same thing in your code, that doesn't mean they do the same
thing always.
2. When do you use the first form and the second form?

When you pass by reference you pass a reference to the original object to
your function, when you pass by value you get a copy of the original object.
There are many different reasons for preferring one over the other.

Here's one example

void f1(char* &s){
s = "a";
}

void f2(char* s){
s = "b";
}

int main()
{
char* s = "c";
cout << s << '\n';
f2(s);
cout << s << '\n';
f1(s);
cout << s << '\n';
}

Try running that and see what difference the reference makes.

john
 
R

Rolf Magnus

Thomas said:
pembed2003 wrote:

I have modified your program, spot the difference in the output.

void f1(char* &s){
s = "abcd";
}

void f2(char* s){
s = "efgh";
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
ITYM:
printf("%s\n",b);
f1(b);
printf("%s\n",a);

printf("%s\n",b);
f2(b);
printf("%s\n",a);

printf("%s\n",b);

return 0;
}
 
T

Thomas Stegen

Rolf said:
Thomas Stegen wrote:
[snip]

Aiaiaiai, let this be a lesson for all, don't modify code
without reading it very very properly.!!!
(See what I did there)
 
P

pembed2003

John Harrison said:
pembed2003 said:
Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}

void f2(char* s){
*s = 'b';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same thing?

They both do the same thing in your code, that doesn't mean they do the same
thing always.
2. When do you use the first form and the second form?

When you pass by reference you pass a reference to the original object to
your function, when you pass by value you get a copy of the original object.
There are many different reasons for preferring one over the other.

Here's one example

void f1(char* &s){
s = "a";
}

void f2(char* s){
s = "b";
}

int main()
{
char* s = "c";
cout << s << '\n';
f2(s);
cout << s << '\n';
f1(s);
cout << s << '\n';
}

Try running that and see what difference the reference makes.

john

Good example. I think I understand it. In f2(), s is a local
automiatic variable, when you say 's = "b";', this local variable is
modified. In f1(), s is a reference to main's s so when you say 's =
"a";', main's s is also changed.
 
J

John Harrison

Good example. I think I understand it. In f2(), s is a local
automiatic variable, when you say 's = "b";', this local variable is
modified. In f1(), s is a reference to main's s so when you say 's =
"a";', main's s is also changed.

You got it.

john
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top