i have to send char array to a function and then return the char array?so what is the method for an char array of size 15?
Printable View
i have to send char array to a function and then return the char array?so what is the method for an char array of size 15?
Do you want to send the contents of the array, or a pointer to it?
i think this should do.
Code:char* myfuntion(const char* your_ variable)
{
// do stuff
return (//your char* value here);
}
int main(void)
{
char v[] = "my array";
myfunction(v);
}
incase of 15 elements just do
Code:char v[15] = "my name is fala";
myfunction(v);
//same as before.
Just use std::string. Otherwise your options are to change the string you are passed or return a dynamically allocated string and remember to dealocate it.
Code:// with std::string, copying string
std::string sixtofour(std::string str) {
for(int i=0;i<length;++i) {
if(str[i] = '6') str[i] = '4';
}
return str;
}
char str[15] = "25 or 624";
cout << sixtofour(str) << endl;
// prints "25 or 424", str unchanged;
//with char[15]; modifying string
char * sixtofour(char str[15]) {
int length = strlen(str);
for(int i=0;i<length;++i) {
if(str[i] = '6') str[i] = '4';
}
return str;
}
cout << sixtofour(str) << endl;
// prints "25 or 424", str="25 or 424" ;
char *s= sixtofour(str);
s[3] = '2'; // changes str!
char *s2 = sixtofour("25 or 6 to 4");// This will often crash many computers
//with char15, copying string.
char *sixtofour(char str[15]) {
int length = strlen(str);
char * copy = new char[length+1];
copy[0] = '\0';
strcat(copy,str);
for(int i=0;i<length;++i) {
if(copy[i] = '6') copy[i] = '4';
}
return copy;
}
char *s = sixtofour(str);
//str unchanged
s[3] = 2;
//still unchanged
//but you need to do some more
delete [] s;
// clean up after sixtofour() solo