Call by Reference with String

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 12
Reputation: Furtano is an unknown quantity at this point 
Solved Threads: 0
Furtano Furtano is offline Offline
Newbie Poster

Call by Reference with String

 
0
  #1
Jul 1st, 2009
Call by Reference with String

Hello,
how can I transfer a String variable with Call by Reference?
Here my code:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. void int_to_hex(int *dez,string hex_string); // Prototype
  7.  
  8. int main(){
  9. string hex_string; //then hex_string becomes a value...
  10. int c = 123;
  11. int_to_hex(int_to_hex(c, hex_string);
  12. return 0;
  13. }
  14.  
  15. void int_to_hex(int *dez,string hex_string){
  16. hex_string.insert(0,"0x");
  17.  
  18. }
Last edited by Furtano; Jul 1st, 2009 at 10:49 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Call by Reference with String

 
0
  #2
Jul 1st, 2009
  1. void int_to_hex(int *dez,string& hex_string);
Last edited by Agni; Jul 1st, 2009 at 10:58 am.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Call by Reference with String

 
0
  #3
Jul 1st, 2009
It is call-by Reference but you didn't use a reference anywhere.

--------- Edit ---------
Heh angi, you posted right before me -.-
Last edited by u8sand; Jul 1st, 2009 at 10:59 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Call by Reference with String

 
0
  #4
Jul 1st, 2009
A simple example demonstrating a string passed by reference:
  1. void func(string &s)
  2. {
  3. s += " something.";
  4. }
This is just a useless function which adds " something." at the end of the string passed to the function, but it demonstrates the use of references.

A call to this function would look like:
  1. string str = "This is";
  2. func(str);
After running the function, str will contain: This is something. .
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 12
Reputation: Furtano is an unknown quantity at this point 
Solved Threads: 0
Furtano Furtano is offline Offline
Newbie Poster

Re: Call by Reference with String

 
0
  #5
Jul 1st, 2009
Thanks for your answers, but it doesnt work.

//edit: It does work, but whats with this warnings ?

  1.  
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. void int_to_hex(string &hex_string); // Prototype
  11.  
  12. int main(){
  13. string hex_string; //then hex_string becomes a value...
  14. hex_string = "test";
  15. int_to_hex(hex_string);
  16. return 0;
  17. }
  18.  
  19.  
  20.  
  21. void int_to_hex(string &hex_string){
  22. hex_string += " works not";
  23. }

  1. chris@chris-desktop:~/Desktop/Programmieren II$ g++ -o -W chris cbr.cpp
  2. chris: In function `_start':
  3. /build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start'
  4. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:/build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:65: first defined here
  5. chris:(.rodata+0x0): multiple definition of `_fp_hw'
  6. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here
  7. chris: In function `_fini':
  8. /build/buildd/glibc-2.9/csu/../sysdeps/generic/initfini.c:109: multiple definition of `_fini'
  9. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crti.o:/build/buildd/glibc-2.9/csu/../sysdeps/generic/initfini.c:109: first defined here
  10. chris:(.rodata+0x4): multiple definition of `_IO_stdin_used'
  11. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
  12. chris: In function `__data_start':
  13. (.data+0x0): multiple definition of `__data_start'
  14. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.data+0x0): first defined here
  15. chris: In function `__data_start':
  16. (.data+0x4): multiple definition of `__dso_handle'
  17. /usr/lib/gcc/i486-linux-gnu/4.3.3/crtbegin.o:(.data+0x0): first defined here
  18. chris: In function `_init':
  19. /build/buildd/glibc-2.9/build-tree/i386-libc/csu/crti.S:15: multiple definition of `_init'
  20. /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crti.o:/build/buildd/glibc-2.9/build-tree/i386-libc/csu/crti.S:15: first defined here
  21. /tmp/cc06n2A3.o: In function `main':
  22. cbr.cpp:(.text+0x77): multiple definition of `main'
  23. chris:(.text+0x14e6): first defined here
  24. /usr/lib/gcc/i486-linux-gnu/4.3.3/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
  25. chris:(.dtors+0x4): first defined here
  26. /usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
  27. /usr/bin/ld: error in chris(.eh_frame); no .eh_frame_hdr table will be created.
  28. collect2: ld gab 1 als Ende-Status zurück
  29.  
Last edited by Furtano; Jul 1st, 2009 at 11:14 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Call by Reference with String

 
0
  #6
Jul 1st, 2009
>It does work, but whats with this warnings ?
According to what I think, your program is bigger than that what you've posted, so could you please post the whole code so we can take a look at it?
The example in your post should compile fine without any warnings.
Last edited by tux4life; Jul 1st, 2009 at 11:26 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 12
Reputation: Furtano is an unknown quantity at this point 
Solved Threads: 0
Furtano Furtano is offline Offline
Newbie Poster

Re: Call by Reference with String

 
1
  #7
Jul 1st, 2009
@tux4life
Aaarg.
Yes sorry your right, now it works very fine.
I used g++ -o -W cbr cbr.cpp
instead of g++ -o cbr -W cbr.cpp

Thanks @ all
Last edited by Furtano; Jul 1st, 2009 at 11:36 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC