Returning a non temporary arrary from function

This is a discussion on Returning a non temporary arrary from function within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a function that takes two array parameters and returns a new character array based on the concatenation ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Returning a non temporary arrary from function

    Hello,

    I have a function that takes two array parameters and returns a new character array based on the concatenation of the first parameter with the second parameter at the end.

    This function works (given below):

    //this function takes two character arrays, concats them and returns a new returnable char array
    char * conCatNew(char f[], char s[])
    {
    //create the temporary array
    char temp[250];

    //copy the first argument then concat the second argument to the temp array
    strcpy(temp, f);
    strcat(temp, s);

    //return the temp array to the invocation
    return temp;

    } //end conCatNew function

    However, my compiler VS .net is giving me a warning:

    dig2.cpp(178) : warning C4172: returning address of local variable or temporary

    I would like to make the temp arrary a permanent return to be used from the place of invocation without this warning.

    Do I need to do a reference return? I do not know how to do this with an array.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Note that char temp[250]; gets destroyed after the function exits. You should rather give the new buffer as an argument to the function:
    void conCatNew(char f[], char s[], char *newstring)

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks. That is a helpful alternative.

    However, here is my function invocation for conCatNew.

    long sHand = _findfirst(conCatNew(curDirArg, "\\*.*"), &fileStruct);

    I need to use it in the _findfirst function. Info for this function is located at:

    http://msdn.microsoft.com/library/de...ndfirsti64.asp

    As you can see, I need to use the returned value as an argument for another function.

    Thanks again.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    If you pass it to another function, you cannot know if it gets destroyed or not. Then you would use char *temp=(char *)malloc(200);
    But you can just create a temporary buffer outside the function and give that as an argument.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.

    //this function takes two character arrays, concats them and returns a new returnable char array
    char * conCatNew(char f[], char s[])
    {
    //create the temporary array
    char *temp;

    temp = new char[250];
    //copy the first argument then concat the second argument to the temp array
    strcpy(temp, f);
    strcat(temp, s);

    //return the temp array to the invocation
    return temp;

    } //end conCatNew function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21