how to return a char array

G

Ger

Hi,

I have been trying to return a data packet (as a char array) at the end of a
function but that seems to be impossible...
I would like to use this char array in a different class..anyone some ideas?

greetz,
Ger
 
N

Nick Hounsome

Ger said:
Hi,

I have been trying to return a data packet (as a char array) at the end of a
function but that seems to be impossible...
I would like to use this char array in a different class..anyone some ideas?

greetz,
Ger

You can either:
1 return a char* pointer to an array (better not be an automatic one).
2 create a struct with an array in it and return one of those (almost
certainly a bad idea)
3. pass in a reference to a std::vector and fill it (pprobably the best bet
without knowing more)
 
O

osmium

Ger said:
I have been trying to return a data packet (as a char array) at the end of a
function but that seems to be impossible...
I would like to use this char array in a different class..anyone some
ideas?

Allocate space for the char array, using new, in some function *before*
calling the 'data packet' function. Pass a pointer to this array around
until its meaningful lifetime has expired. Then delete it with delete. The
assignment and deletion do not have to be performed in the same function.
 
M

Mike Wahler

Ger said:
Hi,

I have been trying to return a data packet (as a char array) at the end of a
function but that seems to be impossible...

Right. Passing and returning arrays to/from functions is
not allowed. Pass a pointer instead.
I would like to use this char array in a different class..anyone some
ideas?

void get_packet(char *arg)
{
strcpy(arg, "data");
some_function_that_gets_packet(arg);
}


int main()
{
char *array = new char[DESIRED_SIZE];
get_packet(array);
cout << array << '\n'; // outputs "data"
}

It's also a good idea to send a second argument as
well: the size of the array, so you can protect
against an overrun.

An alternative to the above is to wrap the array in a
class or struct, and return that. Or store the array
contents in a standard container (e.g. vector) and return
that, and if the caller needs the 'raw' array form, you
can recreate it from the container data.

-Mike
 
K

Kevin Goodsell

osmium said:
Ger writes:



ideas?

Allocate space for the char array, using new, in some function *before*
calling the 'data packet' function. Pass a pointer to this array around
until its meaningful lifetime has expired. Then delete it with delete. The
assignment and deletion do not have to be performed in the same function.

Better use new[] and delete[], not new and delete.

-Kevin
 
B

Bryan Bullard

try to avoid using byte arrays to represent strings in c++. std::string is
safer and very flexible.
 
N

NKOBAYE027

return a reference to a std::vector<char> instead - in C++, unless you
absolutely have no choice, you should never use arrays. arrays are evil -
use standard containers instead.
 
J

Jeff Schwab

return a reference to a std::vector<char> instead - in C++, unless you
absolutely have no choice, you should never use arrays. arrays
are evil - use standard containers instead.

1) That's a load of baloney.

2) Returning a reference to a local vector doesn't
solve the problem. The vector will be destroyed
before the caller gets to use it.

3) Please don't top-post.
 
A

Attila Feher

Jeff said:
you > absolutely have no choice, you should never use arrays. arrays

1) That's a load of baloney.

Actually the "avoid arrays" is a good advice for beginners. You can do
everything with a vector, and more safely. Of course this is no rule for
production code. :)
2) Returning a reference to a local vector doesn't
solve the problem. The vector will be destroyed
before the caller gets to use it.

It's fun! :) (My program works in debug mode, but not in release. Please
help! ;)
3) Please don't top-post.

Agreed!
 
J

Jeff Schwab

Attila said:
Actually the "avoid arrays" is a good advice for beginners. You can do
everything with a vector, and more safely. Of course this is no rule for
production code. :)

You're right: Vectors are, indeed, more appropriate as a learning tool
(and for most production purposes). I'm saddened by the "arrays are the
devil" nonsense, though. :(

It's fun! :) (My program works in debug mode, but not in release. Please
help! ;)

Livin' on the edge, baby! "I like bungee jumping and volcano luging,
and in my spare time I dereference null..."
 
N

NKOBAYE027

ooops forgive the return of reference faux pas...i was thinking the same as
Nick's answer above - hand in a reference to that vector and then modify the
referenced object within the function. shows that we shouldn't type before
we think...

as to the comment arrays are evil, it's a quote from M. Cline, who sits on
the ANSI C++ standards committee, is his C++ FAQ Lite at
http://www.parashift.com/c++-faq-lite/index.html it's meant to be tongue in
cheek and taken with a grain of salt. :eek:) Sorry to have offended any former
C programmers.

regards,
Lup
 
A

Attila Feher

Jeff said:
You're right: Vectors are, indeed, more appropriate as a learning tool
(and for most production purposes).

Well, in production they might happily hide errors as well (if you really do
have a small upper limit). But I would say that one is still better off
using the boost array wrapper than "naked" arrays.
I'm saddened by the "arrays are the devil" nonsense, though. :(

It is "the teaching". It is easier to scare people away from something (cf.
religions) than teach them to be responsible. ;-)
Livin' on the edge, baby! "I like bungee jumping and volcano luging,
and in my spare time I dereference null..."

ROFL!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,183
Latest member
BettinaPol

Latest Threads

Top