Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String, Character Arrays

Status
Not open for further replies.

Rhoon

Programmer
Mar 23, 2001
55
US
I'm trying to return a string or a character array. Whats the best way to do this in C++?

Also, is there a way to make an integer for 4 bits long (max of +- 32). I'm expecting to use a large amount of int's, but I don't want numbers above 20 and the rest of the bits are being wasted.

thanks in advance,

Andrew
 
To return a string or an array is very easy :
simply return(string/array).
This method however will have to construct a new object when returning the object (I assume it is an automatic variable that's being returned).
If this is done on large objects and/or done very often, maybe you would want to pass a reference to the string/array instead.
Also you could do a new(string/array) and return a pointer to that object. This will have to be deleted afterwards.
Quite a few options - Pick one that suits your preferences and performance considerations.

To work with numbers in the 0->20 range, you will need 5 bits.
I would use a character type (1 Byte).
/JOlesen
 
In the class I've built, I'm trying to avoid as many pointers as possible, I'm not a big fan of them. I'm trying to have the return type on the function an array or string.

string function_name(int, int) {
// Code here

} // end function

or

char[] function_name(int, int) {} <<-- the [] gets errors thrown, is the problem with no numbers in the []?
char function_name(int, int) {} <<-- obviously doesn't work.
 
Stick to the string return - it's a good choice for most purposes.

string function_name(int arg, int arg2)
{
string svar;

....

return(svar);
} // end function /JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top