Can anyone explain me the concept of returning a 1-D or 2-D array from a function with example? ?
returning array from an function
Page 1 of 18 Replies - 1553 Views - Last Post: 29 May 2009 - 10:13 AM
Replies To: returning array from an function
#2
Re: returning array from an function
Posted 29 May 2009 - 12:16 AM
http://www.dreaminco...wtopic68140.htm
IDK if this will answer your question, but hopefully it helps.
IDK if this will answer your question, but hopefully it helps.
#3
Re: returning array from an function
Posted 29 May 2009 - 12:42 AM
Made a mistake on earlier post...
sorry.
sorry.
This post has been edited by MC1Ben: 29 May 2009 - 12:54 AM
#4
Re: returning array from an function
Posted 29 May 2009 - 12:53 AM
**Off-topic--> Java seems to mimic C++ quite a bit. Just very minor differences...Only being judged by the above code.
#5
Re: returning array from an function
Posted 29 May 2009 - 12:57 AM
IngeniousHax, on 28 May, 2009 - 11:53 PM, said:
**Off-topic--> Java seems to mimic C++ quite a bit. Just very minor differences...Only being judged by the above code.
Yeah I deleted all that now, my mind was thinking Java, maybe because my final in my Java class is due by the end of next week.
Srry. I know how to display a vector in C/C++ from a function, but it won't help here.
#6
Re: returning array from an function
Posted 29 May 2009 - 01:07 AM
Eh it's straight, happens to the best of us. GL on the final
#7
Re: returning array from an function
Posted 29 May 2009 - 07:03 AM
IngeniousHax, on 28 May, 2009 - 11:16 PM, said:
http://www.dreaminco...wtopic68140.htm
IDK if this will answer your question, but hopefully it helps.
IDK if this will answer your question, but hopefully it helps.
That ia about passing 2-D array into a function ..not returning of 2-D array.
#8
Re: returning array from an function
Posted 29 May 2009 - 07:11 AM
Your array has a name, and your function has a return type. Therefore:
If you want to return a specific item from the array:
I believe these will work.
public ARRAY_TYPE returnArray
{...
return ARRAY_NAME;
}
If you want to return a specific item from the array:
return ARRAY_NAME[row][col];
I believe these will work.
#9
Re: returning array from an function
Posted 29 May 2009 - 10:13 AM
pradosh, on 28 May, 2009 - 11:00 PM, said:
Can anyone explain me the concept of returning a 1-D or 2-D array from a function with example? ?
The main problem with native arrays is that a mention of the name decays into a pointer to the first member of the array. You can't grab the whole block and move it anywhere. If you want to return the whole array, and not just a reference to its first member, you have to embed it into some other structure that can be returned as an aggregate, namely a class or struct. Using std::vector is the easiest way to deal with this problem, and I highly recommend it.
The other problem with returning an array is the lifetime of the object. If you make it a local variable in the function, then it will disappear when the function returns. If you allocate it dynamically in the function, then you have to be careful to delete it sometime. Returning a std::vector by value (not pointer or reference) is also a good way to deal with this problem.
If you really must do it the C way instead of the C++ way, one simple example of returning an array:
typedef int ArrayOf10Int[10];
struct ArrayHolder
{
ArrayOf10Int array;
};
ArrayHolder returnAnArray()
{
ArrayHolder result;
// Note return by value makes a copy of the struct
return result;
}
int main(int argc, char* argv[])
{
ArrayHolder ah = returnAnArray();
ArrayOf10Int& a = ah.array;
a[0] = 99;
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|