Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char*& or char*

Reply
Thread Tools

char*& or char*

 
 
pembed2003
Guest
Posts: n/a
 
      04-15-2004
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!
 
Reply With Quote
 
 
 
 
Thomas Stegen
Guest
Posts: n/a
 
      04-15-2004
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;
}

--
Thomas.


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-15-2004
"pembed2003" <> wrote in message
news: om...
> 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-15-2004
pembed2003 wrote:

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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-15-2004

"pembed2003" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-15-2004
Thomas Stegen wrote:

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



 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      04-15-2004
Rolf Magnus wrote:

> 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)

--
Thomas.

 
Reply With Quote
 
pembed2003
Guest
Posts: n/a
 
      04-15-2004
"John Harrison" <> wrote in message news:<c5ml3o$3g2vb$>...
> "pembed2003" <> wrote in message
> news: om...
> > 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.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-16-2004
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 09:47 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-08-2006 11:14 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57