| | |
Call by Reference with String
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 12
Reputation:
Solved Threads: 0
Call by Reference with String
Hello,
how can I transfer a String variable with Call by Reference?
Here my code:
Hello,
how can I transfer a String variable with Call by Reference?
Here my code:
c++ Syntax (Toggle Plain Text)
#include <cstdio> #include <iostream> #include <sstream> #include <string> void int_to_hex(int *dez,string hex_string); // Prototype int main(){ string hex_string; //then hex_string becomes a value... int c = 123; int_to_hex(int_to_hex(c, hex_string); return 0; } void int_to_hex(int *dez,string hex_string){ hex_string.insert(0,"0x"); }
Last edited by Furtano; Jul 1st, 2009 at 10:49 am.
c++ Syntax (Toggle Plain Text)
void int_to_hex(int *dez,string& hex_string);
Last edited by Agni; Jul 1st, 2009 at 10:58 am.
thanks
-chandra
-chandra
A simple example demonstrating a string passed by reference:
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:
After running the function, str will contain: This is something. .
C++ Syntax (Toggle Plain Text)
void func(string &s) { s += " something."; }
A call to this function would look like:
C++ Syntax (Toggle Plain Text)
string str = "This is"; func(str);
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2009
Posts: 12
Reputation:
Solved Threads: 0
Thanks for your answers, but it doesnt work.
//edit: It does work, but whats with this warnings ?
//edit: It does work, but whats with this warnings ?
c++ Syntax (Toggle Plain Text)
#include <cstdio> #include <iostream> #include <sstream> #include <string> #include <cstring> using namespace std; void int_to_hex(string &hex_string); // Prototype int main(){ string hex_string; //then hex_string becomes a value... hex_string = "test"; int_to_hex(hex_string); return 0; } void int_to_hex(string &hex_string){ hex_string += " works not"; }
C++ Syntax (Toggle Plain Text)
chris@chris-desktop:~/Desktop/Programmieren II$ g++ -o -W chris cbr.cpp chris: In function `_start': /build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start' /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 chris:(.rodata+0x0): multiple definition of `_fp_hw' /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here chris: In function `_fini': /build/buildd/glibc-2.9/csu/../sysdeps/generic/initfini.c:109: multiple definition of `_fini' /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 chris:(.rodata+0x4): multiple definition of `_IO_stdin_used' /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here chris: In function `__data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.data+0x0): first defined here chris: In function `__data_start': (.data+0x4): multiple definition of `__dso_handle' /usr/lib/gcc/i486-linux-gnu/4.3.3/crtbegin.o:(.data+0x0): first defined here chris: In function `_init': /build/buildd/glibc-2.9/build-tree/i386-libc/csu/crti.S:15: multiple definition of `_init' /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 /tmp/cc06n2A3.o: In function `main': cbr.cpp:(.text+0x77): multiple definition of `main' chris:(.text+0x14e6): first defined here /usr/lib/gcc/i486-linux-gnu/4.3.3/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__' chris:(.dtors+0x4): first defined here /usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. /usr/bin/ld: error in chris(.eh_frame); no .eh_frame_hdr table will be created. collect2: ld gab 1 als Ende-Status zurück
Last edited by Furtano; Jul 1st, 2009 at 11:14 am.
>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.
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."
![]() |
Similar Threads
- Call by reference question? (C++)
- Quick question, call by reference? (C)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: The Hello World server/client program
- Next Thread: C++ MMORPG Maker
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






