i want to return a string array so i can use it in my main function. how do i do this?

#include <iostream>
#include <string>
using namespace std;

string test()
{
    string a[5] = {"Hello","World","How","Are","You"};

    return a;
}


int main()
{
    string b[5];

    b=test();

    for(int i=0;i<5;i++)
        cout << b[i] << endl;

    return 0;
}

Recommended Answers

All 12 Replies

Why don't you pass the array as an argument to the function instead?

//...

void test(string a[5])
{
    a[0] = "Hello";

    //...
}

int main()
{
    string b[5];

    test(b);

    //...
}

several things have gone wrong here.
But my first suggestion is use stl::vector

See inline commands below:

#include <iostream>
#include <string>
using namespace std;

string test() 
{
    string a[5] = {"Hello","World","How","Are","You"};

    return a; //You are trying to return a local variable, once local variable out of scope, programs frees the memory, so you are returning an invalid memory.
}


int main()
{
    string b[5];

    b=test(); //You can not assign an array like this.

    for(int i=0;i<5;i++)
        cout << b[i] << endl;

    return 0;
}

Now solution of your problem is, First read about local variable and pointer/reference of local variable.

second, there is a relation between an array and a pointer, you can refer an one dimensional array by pointer.
two dimensional array can be refered as double pointer and so on..

#include <iostream>
#include <string>
using namespace std;

string *test() //Notice the pointer..
{
    string a[5] = {"Hello","World","How","Are","You"};

    return a;  //still you are returning an invalid memory as end of this method, this memory will be invalid.
}


int main()
{

    string *b=test(); //Notice the pointer to catch the array,

    for(int i=0;i<5;i++)
        cout << b[i] << endl; //This will get crash as b is pointing to an invalid memory

    return 0;
}

you need to read about scope of variable.

I hope it helps.

i want to return a string array so i can use it in my main function. how do i do this?

You don't. Arrays aren't assignable, which means you'd be returning a pointer to either the array itself or a pointer to the first element. Since a local array is destroyed when the function returns, that gives you a dangling pointer. Very bad.

How about using a std::vector or std::array (tr1::array) object instead?

#include <array>
#include <iostream>
#include <string>

using namespace std;

array<string, 5> test()
{
    array<string, 5> a = {"Hello","World","How","Are","You"};

    return a;
}

int main()
{
    array<string, 5> b;

    b = test();

    for (int i = 0; i < b.size(); i++)
        cout << b[i] << endl;
}

instead of doing it this way what other method can i use..

i want a function to do some modification to an string array and pass it back...

Ok, I'll play along.

#include <boost/shared_array.hpp>
#include <iostream>
#include <string>

boost::shared_array<std::string> test()
{
    boost::shared_array<std::string> array(new std::string[5]);

    array[0] = "Hello";
    array[1] = "World";
    array[2] = "How";
    array[3] = "Are";
    array[4] = "You";

    return array;
}

int main()
{
    boost::shared_array<std::string> b;

    b = test();

    for(int i = 0; i < 5; ++i)
        std::cout << b[i] << std::endl;

    return 0;
}

EDIT:

i want a function to do some modification to an string array and pass it back...

Did you see my first reply?

Ok, I'll play along.

#include <boost/shared_array.hpp>
#include <iostream>
#include <string>

boost::shared_array<std::string> test()
{
    boost::shared_array<std::string> array(new std::string[5]);

    array[0] = "Hello";
    array[1] = "World";
    array[2] = "How";
    array[3] = "Are";
    array[4] = "You";

    return array;
}

int main()
{
    boost::shared_array<std::string> b;

    b = test();

    for(int i = 0; i < 5; ++i)
        std::cout << b[i] << std::endl;

    return 0;
}

EDIT:

Did you see my first reply?

i tried your code...it doesn't work...

if i use a void function how can i use the modified array in the main function?

The fact that the function doesn't return anything is irrelevant here.
You can pass your array as an argument, modify it, and the changes
you make will be reflected in main. This is the default behaviour
for arrays. I assure you it will work if you fill in the missing code.
I have tried it and it works perfectly fine for me.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int main()
{
    int num,count=0;
    int temp[52];
    bool check;

    /* initialize random seed: */
    srand(time(0));
    /* generate secret number: */

    while(count != 52)
    {
        num = rand()%52+1;
        check=true;
        for(int i=0;i<52;i++)
        {
            if(num == temp[i])
                check = false;
        }
        if(check)
        {
            temp[count] = num;
            count++;
        }
    }

    string deck[52] = { "H*A","H*2","H*3","H*4","H*5","H*6","H*7","H*8","H*9","H*10","H*J","H*Q","H*K",
                        "D*A","D*2","D*3","D*4","D*5","D*6","D*7","D*8","D*9","D*10","D*J","D*Q","D*K",
                        "C A","C 2","C 3","C 4","C 5","C 6","C 7","C 8","C 9","C*10","C J","C Q","C K",
                        "S A","S 2","S 3","S 4","S 5","S 6","S 7","S 8","S 9","S*10","S J","S Q","S K"};

    string card[52];

    for(int i=0;i<52;i++)
    {
        card[i] = deck[temp[i]-1];
    }
}

this is a card shuffling code...i want to put this in a function and return string card so i can use it in my main function...how do i do that...

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void shuffle(const string deck[52], string card[52])
{
    // build your temp array here

    for(int i = 0; i < 52; ++i)
        card[i] = deck[temp[i] - 1];
}

int main()
{
    string deck[52] = { "H*A","H*2","H*3","H*4","H*5","H*6","H*7","H*8","H*9","H*10","H*J","H*Q","H*K",
                        "D*A","D*2","D*3","D*4","D*5","D*6","D*7","D*8","D*9","D*10","D*J","D*Q","D*K",
                        "C A","C 2","C 3","C 4","C 5","C 6","C 7","C 8","C 9","C*10","C J","C Q","C K",
                        "S A","S 2","S 3","S 4","S 5","S 6","S 7","S 8","S 9","S*10","S J","S Q","S K"};
    string card[52];

    shuffle(deck, card);
}

EDIT: You could also put deck inside the function...

//...

void shuffle(string card[52])
{
    static const string deck[52] = { /*...*/ };

    // build your temp array here

    for(int i = 0; i < 52; ++i)
        card[i] = deck[temp[i] - 1];
}

int main()
{
    string card[52];

    shuffle(card);
}

it work...thank alot m4ster_r0shi...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.