943,763 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1484
  • C++ RSS
Jul 1st, 2009
0

Call by Reference with String

Expand Post »
Call by Reference with String

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

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
Furtano is offline Offline
12 posts
since Jul 2009
Jul 1st, 2009
0

Re: Call by Reference with String

c++ Syntax (Toggle Plain Text)
  1. void int_to_hex(int *dez,string& hex_string);
Last edited by Agni; Jul 1st, 2009 at 10:58 am.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 1st, 2009
0

Re: Call by Reference with String

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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 1st, 2009
0

Re: Call by Reference with String

A simple example demonstrating a string passed by reference:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. string str = "This is";
  2. func(str);
After running the function, str will contain: This is something. .
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 1st, 2009
0

Re: Call by Reference with String

Thanks for your answers, but it doesnt work.

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

c++ Syntax (Toggle Plain Text)
  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. }

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
Furtano is offline Offline
12 posts
since Jul 2009
Jul 1st, 2009
0

Re: Call by Reference with String

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 1st, 2009
1

Re: Call by Reference with String

@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.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
Furtano is offline Offline
12 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: The Hello World server/client program
Next Thread in C++ Forum Timeline: C++ MMORPG Maker





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC