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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |