Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

Returning arrays from a function in c++

I am trying to return an array from a function:

#include <iostream>

using namespace std;
int* uni(int *a,int *b)
{
    int c[10];
    int i=0;
    while(a[i]!=-1)
    {
        c[i]=a[i];
        i++;
    }
    for(;i<10;i++)
        c[i]=b[i-5];

    return c;
}
int main()
{
    int a[10]={1,3,3,8,4,-1,-1,-1,-1,-1};
    int b[5]={1,3,4,3,0};
    int *c=uni(a,b);
    for(int i=0;i<10;i++)
        cout<<c[i]<<" ";
    cout<<"
";

    return 0;
}

I pass two arrays from my main() into my uni() function. There I create a new array c[10] which I return to my main(). In my uni() function I try to merge the non-negative numbers in the two arrays a and b.

But I get something like this as my output.

1 -1078199700 134514080 -1078199656 -1216637148 134519488 134519297 134519488 8 -1078199700 

Whereas when I try to print the values of c[10] in the uni() function it prints the correct values. Why does this happen?? Is this something related to the stack?? Because I have tried searching about this error of mine, and I found a few places on stackoverflow, where it says that do not allocate on stack but I couldn't understand it.

Further it would become very easy if I allocate my array globally, but if this is the case then everything shall be declared globally?? Why are we even worried about passing pointers from functions?? (I have a chapter in my book for passing pointers)

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Admittedly, the std::vector or std::array approach would be the way to go.

However, just to round things out (and if this is a school project, where the teacher gives you the obligatory "you can't use STL"), the other alternative that will avoid pointer usage is to wrap the array inside a struct and return the instance of the struct.

#include <iostream>

using namespace std;
struct myArray
{
   int array[10];
};

myArray uni(int *a,int *b)
{
    myArray c;
    int i=0;
    while(a[i]!=-1)
    {
        c.array[i]=a[i];
        i++;
    }
    for(;i<10;i++)
        c.array[i]=b[i-5];
    return c;
}

int main()
{
    int a[10]={1,3,3,8,4,-1,-1,-1,-1,-1};
    int b[5]={1,3,4,3,0};
    myArray c = uni(a,b);
    for(int i=0;i<10;i++)
        cout << c.array[i] << " ";
    cout << "
";
    return 0;
}

Note that the struct is returned by value, and this return value is assigned in main.

You have the value semantics of returning an instance, plus the struct will get copied, including the array that is internal within it.

Live Example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...