Is It Possible to Return a String from a Function ?

Is It Possible to Return a String from a Function ?

Post by charle » Fri, 21 Jul 2000 04:00:00



Quote:>Is it possible to return a string from a function such that the function
>prototype would look as follows :
>    char[20] functionname (int) ;

char* functionname(int)

void main( void )
{
    char * temp;
    temp = functionname( 1 );

Quote:}

char * functionname( int i )
{
    if( i == 1 )
        return "something";
Quote:}

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> >Is it possible to return a string from a function such that the
function
> >prototype would look as follows :
> >    char[20] functionname (int) ;

> char* functionname(int)

> void main( void )

Yechhh!...

Quote:> {
>     char * temp;
>     temp = functionname( 1 );

> }

> char * functionname( int i )
> {
>     if( i == 1 )
>         return "something";
> }

The problem is, "something" has type const char*, and you're implicitly
casting it to char*.  As soon as somebody out there tries to change the
contents of the "array" - BOOM!!! - memory access violation.  The right
way could be returning a pointer generated by 'new', a pointer to a
static memory, or a pointer that was passed in for changes.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> Is it possible to return a string from a function such that the
function
> prototype would look as follows :
>     char[20] functionname (int) ;

> Or is the only solution to pass the array into the function as an
argument
> (seeing as it will be pass by reference)

Do you really need arrays?  You're in C++.  You have std::basic_string
template, you have derived from it std::string class.  Why bother with
char arrays at all?

And answering your question, no, an array cannot be a return type.
There are different ways to do what you want to do and they all are
really no good.  Use std::string.  Trust me, it's so much better.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by Rocky Raccoo » Fri, 21 Jul 2000 04:00:00



> Is it possible to return a string from a function such that the function
> prototype would look as follows :
>     char[20] functionname (int) ;

> Or is the only solution to pass the array into the function as an argument
> (seeing as it will be pass by reference)

> Thanks for any info on this

> JB

Check the clc FAQ
http://www.eskimo.com/~scs/C-faq/q7.5.html
 
 
 

Is It Possible to Return a String from a Function ?

Post by Matthew Auster » Fri, 21 Jul 2000 04:00:00



> Is it possible to return a string from a function such that the function
> prototype would look as follows :
>     char[20] functionname (int) ;

> Or is the only solution to pass the array into the function as an argument
> (seeing as it will be pass by reference)

Two ways of doing this:

    (1)
    char* functionname(int n) {
      char* result = new char[n];    // Caller must deallocate storage
      ...
      return result;
    }

    (2)
    std::string functionname(int);

Option (2) is simpler, of course.  C++ has a large library; you should
use it.

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> Many thanks charles, works perfectly and I've learnt one more thing in
the
> long quest that is c++.

No offence to you or to "charles", but there is no C++ in
what "charles" wrote and you "learnt".  It violates C++
Standard at least once, has a syntax error (no semicolon
after the prototype) and a logical error (not all control
paths in 'functionname' function return a value).  I would
strongly advise you to take this source code VERY CAUTIOUSLY.

I do hope that "Micron Semiconductor, Inc." doesn't rely in
its business on code like that.


> > Organization: Micron Semiconductor, Inc.
> > Newsgroups: comp.lang.c++
> > Date: Thu, 20 Jul 2000 15:58:15 -0600
> > Subject: Re: Is It Possible to Return a String from a Function ?

> >> Is it possible to return a string from a function such that the
function
> >> prototype would look as follows :
> >> char[20] functionname (int) ;

> > char* functionname(int)

> > void main( void )
> > {
> > char * temp;
> > temp = functionname( 1 );

> > }

> > char * functionname( int i )
> > {
> > if( i == 1 )
> > return "something";
> > }

Victor
--
Please remove capital A's from my address when replying by mail
 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> >The problem is, "something" has type const char*,

> But the cast was implicit. I've noticed lots of compilers do not warn
when
> this happens. Is this so by ANSI?

Yes, unfortunately, the Standard allows initialisation of a char* with
a string literal.  Compatibility with old code, they say.  Compatibility
my back!  Bad idea!!  Oh, well...

Quote:

> >The right
> >way could be returning a pointer generated by 'new', a pointer to a
> >static memory, or a pointer that was passed in for changes.

> I usually return a string object (take your pick) when faced with this
sort
> of thing.

That's probably the best C++ lets you do.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by charle » Fri, 21 Jul 2000 04:00:00


Quote:>Do you really need arrays?  You're in C++.  You have std::basic_string
>template, you have derived from it std::string class.  Why bother with
>char arrays at all?

Perhaps Judy is a student?

later,
charles

 
 
 

Is It Possible to Return a String from a Function ?

Post by charle » Fri, 21 Jul 2000 04:00:00


Quote:>The problem is, "something" has type const char*, and you're implicitly
>casting it to char*.  As soon as somebody out there tries to change the
>contents of the "array" - BOOM!!! - memory access violation.  The right
>way could be returning a pointer generated by 'new', a pointer to a
>static memory, or a pointer that was passed in for changes.

true, my bad, thanks for the catch.

later,
charles

 
 
 

Is It Possible to Return a String from a Function ?

Post by charle » Fri, 21 Jul 2000 04:00:00


Quote:>No offence to you or to "charles", but there is no C++ in
>I do hope that "Micron Semiconductor, Inc." doesn't rely in
>its business on code like that.

heh heh... it was hastily done and I am sorry.  There is no need to flame
Micron for it.  I made a couple mistakes... I was just trying to show it
could be done Victor.  I'm sure that you have made mistakes before and I
would not flame your organization for it.

later,
charles

 
 
 

Is It Possible to Return a String from a Function ?

Post by charle » Fri, 21 Jul 2000 04:00:00


Although he was less than polite Victor was right.  I believe the correct
everything for what you would want to do would be something like this.

#include <stdio.h>
#include <string.h>

char * ReturnString( int );

int main( void )
{
    char * ReturnedString = NULL;

    ReturnedString = ReturnString( 1 );

    delete ReturnedString;
    ReturnedString = NULL;
    return 0;

Quote:}

char * ReturnString( int value )
{
    char * temp = new char[ 100 ];

    if( value == 1 )
    {
        strcpy( temp, "one" );
        return temp;
    }
    else
    {
        strcpy( temp, "" );
        return temp;
    }

Quote:}

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> >No offence to you or to "charles", but there is no C++ in
> >I do hope that "Micron Semiconductor, Inc." doesn't rely in
> >its business on code like that.

> heh heh... it was hastily done and I am sorry.  There is no need to
flame
> Micron for it.  I made a couple mistakes... I was just trying to show
it
> could be done Victor.  I'm sure that you have made mistakes before and
I
> would not flame your organization for it.

I didn't flame Micron.  But I really would recommend you either
to put a disclaimer saying that Micron has nothing to do with what
you post or just remove any reference to Micron from your settings.

And, of course, I do make mistakes too.  However, I represent only
myself here.  You won't find any reference to my organisation (at
least I hope you won't :-).  They tolerate that.  I appreciate it.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> Although he was less than polite Victor was right.  I believe the
correct
> everything for what you would want to do would be something like this.

Close.  No cigar, though.

Quote:

> #include <stdio.h>
> #include <string.h>

It is better to use <cstdio> and <cstring>.

Quote:

> char * ReturnString( int );

> int main( void )
> {
>     char * ReturnedString = NULL;

>     ReturnedString = ReturnString( 1 );

No need to make it a separate call.  Just initialise from the
returned value:

    char * ReturnedString = ReturnString( 1 );

Quote:

>     delete ReturnedString;

    delete[] ReturnedString;

Quote:>     ReturnedString = NULL;

That is not necessary.

Quote:>     return 0;
> }

> char * ReturnString( int value )
> {
>     char * temp = new char[ 100 ];

>     if( value == 1 )
>     {
>         strcpy( temp, "one" );
>         return temp;
>     }
>     else
>     {
>         strcpy( temp, "" );
>         return temp;
>     }

> }

Victor
--
Please remove capital A's from my address when replying by mail
 
 
 

Is It Possible to Return a String from a Function ?

Post by Arche » Fri, 21 Jul 2000 04:00:00


Quote:> > #include <stdio.h>
> > #include <string.h>

> It is better to use <cstdio> and <cstring>.

Why?

Quote:> >     char * ReturnedString = NULL;

> >     ReturnedString = ReturnString( 1 );

> No need to make it a separate call.  Just initialise from the
> returned value:

Is it not a good idea to always initialize variables when they are declared?

Quote:> >     ReturnedString = NULL;

> That is not necessary.

Is it not a good idea to always set pointers to NULL?
 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Fri, 21 Jul 2000 04:00:00



> > > #include <stdio.h>
> > > #include <string.h>

> > It is better to use <cstdio> and <cstring>.

> Why?

Because it's C++ way.  Using <stdio.h> and <string.h> is a C way.

Quote:

> > >     char * ReturnedString = NULL;

> > >     ReturnedString = ReturnString( 1 );

> > No need to make it a separate call.  Just initialise from the
> > returned value:

> Is it not a good idea to always initialize variables when they are
declared?

> > >     ReturnedString = NULL;

> > That is not necessary.

> Is it not a good idea to always set pointers to NULL?

Not right before the pointer goes out of scope.

Victor
--
Please remove capital A's from my address when replying by mail