Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to return a char array

Reply
Thread Tools

how to return a char array

 
 
Ger
Guest
Posts: n/a
 
      01-26-2004
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




 
Reply With Quote
 
 
 
 
Nick Hounsome
Guest
Posts: n/a
 
      01-26-2004

"Ger" <(E-Mail Removed)> wrote in message
news:bv3hfo$3se$(E-Mail Removed)...
> 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)


 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      01-26-2004
Ger writes:

> 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.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-26-2004
"Ger" <(E-Mail Removed)> wrote in message
news:bv3hfo$3se$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      01-26-2004
osmium wrote:

> Ger writes:
>
>
>>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.
>
>


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

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Bryan Bullard
Guest
Posts: n/a
 
      01-26-2004

"Ger" <(E-Mail Removed)> wrote in message
news:bv3hfo$3se$(E-Mail Removed)...

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

> 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
>
>
>
>



 
Reply With Quote
 
NKOBAYE027
Guest
Posts: n/a
 
      01-27-2004
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.

"Ger" <(E-Mail Removed)> wrote in message
news:bv3hfo$3se$(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      01-27-2004

> "Ger" <(E-Mail Removed)> wrote in message
> news:bv3hfo$3se$(E-Mail Removed)...
>
>>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


NKOBAYE027 wrote:
> 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.

 
Reply With Quote
 
Attila Feher
Guest
Posts: n/a
 
      01-27-2004
Jeff Schwab wrote:
> NKOBAYE027 wrote:
> > 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.


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!

--
Attila aka WW


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      01-27-2004
Attila Feher wrote:
> Jeff Schwab wrote:
>
>>NKOBAYE027 wrote:
>> > 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.

>
>
> 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.


>>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!


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

>>3) Please don't top-post.

>
>
> Agreed!
>
> --
> Attila aka WW
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments