dunno how to pass a char array as a reference

HJB417

Senior member
Dec 31, 2000
763
0
0
I need help trying to pass the memory address of a character array, a reference. I can't get it to work but here's an example with an int.


void change(&int new_value)
{
new_value = 10;
return;
}

void main(void)
{
int value = 9;
change(value);
return
}

simple with int, float, char, and string but I don't know how to do this with arrays, especially char arrays. I tried char* newvalue, char& newvalue[] and all sorts of combinations but I can't get it to compile =(

:(
 

tank3544

Junior Member
Jan 6, 2002
10
0
0
have you gotten into pointers yet? i'm not sure i really remember without them haha
 

HJB417

Senior member
Dec 31, 2000
763
0
0
ya, I know how to use pointers. I don't think I ever passed an array as a reference though.
 

HJB417

Senior member
Dec 31, 2000
763
0
0
#include <iostream>
#include <string>

using namespace std;

void test(char* umm)
{
cout << "umm: " << umm << endl;
umm = "umm";
// *umm[] = "ok, right";
cout << "umm: " << umm << endl;
return;
}

void main(void)
{
char temp[100] = "this is a test";
test(temp);
// the value of temp should be "umm".
cout << endl << "umm: " << temp << endl;



try getting that to work, the last line returns "this is a test" instead of "umm". The function test() is supposed to change the the value of temp. and the very last cout should output "umm" instead of "this is a test".
 

CyrusTCH

Member
Feb 4, 2001
40
0
0
#include <iostream>
#include <string>

using namespace std;

void test(char* umm[])
{
cout << "umm: " << *umm << endl;

char *temp;
temp = new char;

temp = "Two";

*umm = temp;
}

void main(void)
{
char *temp;
temp = new char[100];
temp = "One";

test(&temp);

cout << endl << "umm: " << temp << endl;
}
 

HJB417

Senior member
Dec 31, 2000
763
0
0
OMG You own, thx greatly, I didn't know I had to treat it as a pointer and use new/delete.
 

Cyph3r

Senior member
Jan 20, 2000
790
0
0
BTW, in your initial post you are not passing value by reference, but rather by value. If you did "change (&value);", then you would passing it by reference..

You don't necessarily need to use new/delete for a array, and not usually with static values. If you have an idea of how big your array shoud be, statically define it. So, char x[50]; would work fine. Now just pass "x" to your function that now accepts (char*) as a parameter..Regards
 

Palek

Senior member
Jun 20, 2001
937
0
0
Somebody correct me if I am wrong, but I thought passing by reference worked like this:

********************

#include <iostream>

void change(int& value)
{
value = 10;
}

void main()
{
int value = 5;
change(value);
cout << "value = " << value << endl;
}

******************

The output should be 10.
 

Palek

Senior member
Jun 20, 2001
937
0
0
This is one way of how passing an array might work (just compiled and tested it).

*******************

#include <iostream>

void change(int value[])
{
value[0] = 1;
}

void main()
{
int value[4] = { 5, 4, 3, 2};
change(value);
cout << "value = " << value[0] << endl;
}

******************
<edit>
Oooppss, forgot. Output will be 1.
</edit>
 

Palek

Senior member
Jun 20, 2001
937
0
0
Sorry, forgot that you wanted a character array. With an array of characters the situation is a little different. If you have an array of characters, say char str[100], trying to change the value of the array through substitution using the pointer to the array (i.e. str = "blabla") will not work. You have to use the strcpy function for this. (If you use the CString class of C++ instead of a character array, you can substitute all you want.) Something like this.

******************************

#include <iostream>
#include <string>

void change(char value[])
{
char tmp[10] = "boo yeah";
strcpy(value, tmp);
}

void main()
{
char value[100] = "arriba!!";
change(value);
cout << "value = " << value << endl;
}

******************************

The output will be "boo yeah".
 

HJB417

Senior member
Dec 31, 2000
763
0
0


<< Sorry, forgot that you wanted a character array. With an array of characters the situation is a little different. If you have an array of characters, say char str[100], trying to change the value of the array through substitution using the pointer to the array (i.e. str = "blabla") will not work. You have to use the strcpy function for this. (If you use the CString class of C++ instead of a character array, you can substitute all you want.) Something like this.

******************************

#include <iostream>
#include <string>

void change(char value[])
{
char tmp[10] = "boo yeah";
strcpy(value, tmp);
}

void main()
{
char value[100] = "arriba!!";
change(value);
cout << "value = " << value << endl;
}

******************************

The output will be "boo yeah".
>>





palek, you the man. I never knew about the strcpy function. Where do you find out about these things ?!?!
and you could make the code even shorter

Code:
******************************

#include <iostream>
#include <string>

void main()
{
	char value[100] = "arriba!!";
	strcpy(value, "boo yeah");
	cout << "value = " << value << endl;
	return;
}

*******************************

but regardless of code, it performs the same function. Thxs greatly Palek. I hate reinventing the wheel.