Call by Reference with String

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

#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");

}

Recommended Answers

All 6 Replies

void int_to_hex(int *dez,string& hex_string);

It is call-by Reference but you didn't use a reference anywhere.

--------- Edit ---------
Heh angi, you posted right before me -.-

A simple example demonstrating a string passed by reference:

void func(string &s)
{
    s += " something.";
}

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:

string str = "This is";
func(str);

After running the function, str will contain: This is something. .

Thanks for your answers, but it doesnt work.

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

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

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

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

commented: You're welcome, and well-done on the code tags :) +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.