return in a function

This is a discussion on return in a function within the C++ Programming forums, part of the General Programming Boards category; how should i declare the function if i want it to return a value like char text[10]; i have tried ...

  1. #1
    Unregistered
    Guest

    return in a function

    how should i declare the function if i want it to return a value like

    char text[10];

    i have tried this but it does not work and i dont get what im doing wrong...

    char functiontest()
    {
    char text[10];
    //i do put a value in there but lets keep it simple in here
    return (text);
    }

    arghhhhh what is wrong?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    text is a local variable to functiontest()... when the function returns, text is out of scope and is destroyed... that's why you can return it. can't return a string by value.. you have to use malloc or something like that to put it on the heap so youcan passit back...

    there are a few ways to do it.. so read some tutorials and you'll figure it out.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    I'm not sure about this one, but you could do something like this:

    Code:
    
    typedef char[10] mystring;
    
    mystring myfunc()
    {
      mystring anotherstring;
      return anotherstring;
    }
    
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I always thought u could return an array. Does this work. I do not have a compiler here.


    char [] function()
    {return char array[];}

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can't return an array from a function unless you either return a pointer to the array, such as char * or wrap the array in a struct.
    Code:
    struct Array{
      int myArray[20];
    };
    
    struct Array myFunc(int someArray[]);
    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    Nope. You would need to do the pointer thingy and use a variable declared on the heap, not local.

    Let's say you wanted to change a string in a function. The options are to pass the string to the function as a pointer/array

    int main()
    {
    char dummy[10] = "now";
    foo(dummy);
    cout << dummy;
    .
    .
    .
    return 0;
    }

    void foo(char * _dummy) //or void foo(char [] _dummy)
    {
    strcpy(_dummy, "later");
    }

    or to declare the return string using new (or malloc) in the function itself.

    int main()
    {
    char dummy[10] = "now";
    strcpy(dummy, bar());
    cout << dummy;
    .
    .
    delete [] dummy2;//delete dummy2 heere to free memory declared in bar().
    .
    .
    return 0;
    }


    char * bar()
    {
    char * dummy2 = new char[10];
    strcpy(dummy2, "later");
    return dummy2;
    }

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    I made a test on this. You can't return the array directly, but if you store it into a struct you can (like Prelude said). However, if you're going to use this array as a string (char text[10]... hm ) it is better to return a pointer.

    Code:
    
    #include <conio.h>
    #include <iostream.h>
    
    //Structure containing an array
    typedef struct
    {
       char Nr[10];
    }CharArray;
    
    //Function that return the array-structure
    CharArray MyFunc()
    {
       CharArray MyTempArray;
       for(int i=0; i<10; i++) MyTempArray.Nr[i]=i;
       return MyTempArray;
    }
    
    //Main function
    int main()
    {
       CharArray MyArray=MyFunc();
       for(int i=0; i<10; i++) cout << (int)MyArray.Nr[i] << " ";
       getch();
       return 0;
    }
    
    Last edited by Magos; 01-24-2002 at 10:40 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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