How To Return Many "Char Data Type"

I have this project to work on, and in one of my function, I want to return more than one "char data type"...

I have tried this,
char* myFunction(char*&)

^^^which I am trying to return the data through parameter passing. Is it possible ? because I can't run the code...

Or maybe I should use arrays instead? It that so... how?

Thank you in advance.
Last edited on
yes, char * works.

char * foo()
{
char * cp = new char[10];
...//fill in the data
return cp; //
}

and the other works too

void foo(char *& x)
{

x = new char[10];
//fill in x
}

best to use vector or string. vector if its logically an 'array of bytes' and string if its logically 'text'.

Last edited on
Sorry I am a little bit slow here :)

but, what is the use of "new"...

and you said that it is better to use vector or string... how is that.


and, just came to my mind...
I have this idea to declare a global variable to pass datas... what do you think?
Global vars are not a good idea a to use.
http://www.learncpp.com/cpp-tutorial/4-2a-why-global-variables-are-evil/

new is used to dynamically allocate memory. The problem with this approach is that you have to remember to call delete otherwise you will have memory leaks. Best to avoid it.

Easiest is to use a string.
http://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
std::string foo()
{
   std::string s("some value);

  return s;
}

void foo(std::string& s)
{
   // use s
} 
Ouh ...ok... Will try that ...Thanks Thomas1965
globals are terrible. They make things easy for tiny programs and simple problems but the second you get into a large amount of code, they are impossible to deal with, and real world problems are rarely small. Therefore they are a bad habit that you can get away with in school sized problems but they will hurt you repeatedly in professional sized code. Don't do it :)

Class member variables behave like a global on a smaller, controlled scope. Use of them is similar, making the local / small scale problem that the class solves easier to code, but they are harder to abuse (almost but not quite impossible if you follow modern designs).

Outside of class members, most globals and global like things are usually a red flag of a design problem.
Topic archived. No new replies allowed.