Write a function that returns an array as result
-
QUESTION: Hi, How can I write a function that it can return an array as result? because of most of programs that I have seen just returns a single value as result. Can you help me how i can do it? If I must use pointers, I'll be thankful if you give me an example of such function. thank you ANSWER: Hello Amir. There are 2 ways of returning an array from a function. The most common way is to allocate memory with new for C++ or malloc for C. For example, to return an array of integers you could do this: int* myFunction() { int* result = new int[32]; // Allocate space for 32 integers result[0] = 0; // Notice how the result pointer can be treated as an array. return result; } This function allocates space for 32 integer and returns a pointer to the allocated space. Pointers can be treated like arrays. The problem is that the receiver has to know the amount of space allocated. This is always a problem with arrays. Proper programming requires that the receiver of the array call delete in C++ or free in C to return the memory after the receiver is done with it. The second way is to declare a static array in the function and return that. Like this: int* myFunction() { static int result[32]; result[0] = 0; return result; } Again, you return a pointer. The static keyword causes the memory of the array to be remain valid after the function returns. Without the static keyword, the result memory would become invalid after the function returns. In this case, the receiver of the function should never free the memory. The receiver does not need to worry about freeing memory, and this may seem like an advantage, but, whenever the function is called, the same memory is reused. This may lead to bugs if the function is called multiple times and the receivers expect their array to be unchanged. As I said, the problem with arrays is that you need to know their size. If you are using C++, it is much safer to use vectors because the vector carries its size information with it. I hope that helps you. ---------- FOLLOW-UP ---------- QUESTION: Hello again, Dear Zlatko; thank you for your answer. But still I have a small problem in assigning the returned address of function to an array. For example suppose we want to read some integers by a function and then assign them to an array(instead of direct assigning). I used a static memory and wrote such program: int* myfunction(); int main() { int array[4]; array=*myfunction(); getch(); return 0; } //*************************** int* myfunction() { int i; static int result[4]; for(i=0;i >result[i]; return result; } I expect that *myfunction() returns the value of the pointer to array but it doesn't work. Is this way of assigning wrong for an array? How can i do that? Best regard Amir ANSWER: Hello Amir. The syntax in main should be like this: int* result; result=myfunction(); There should be no '*' in front of myFunction where it is being called. Also, I think the word array may be a reserved in the dot net version of c++ so you might want to name your array variable something else if you are using Microsoft compilers. ---------- FOLLOW-UP ---------- QUESTION: Hello. thanks for your last guides. I've just faced with new situation of previous problem. I want to know the syntax for the previous problem when the dimension of returned array is 2 or higher. for example suppose we want to have a function that it be able to get a 2dimension array as input, add 1 unit to each number and then return the result to the main program. what is wrong in this program(arr1 is the input array and the result must assigned to arr2): int* myfunction(int inp[2][3]); int main() { int i,j; int arr1[2][3]={{1,2,1}, {2,1,0}}; int* arr2; arr2=myfunction(arr1); delete myfunction(arr1); getch(); return 0; } //*************************** int* myfunction(int inp[2][3]) { int i,j; int* result=new int[2][3]; for(i=0;i<2;i++) for(j=0;j<3;j++) result[i][j]=inp[i][j]+1; return result; } Best Regards Amir
-
Answer:
Hello Amir. Sorry, there is no pretty answer to this question. When you allocate memory with new, you get back a length of bytes in memory. The language does not have a built in way to treat the memory as a two dimensional array. When you write new int[2][3], the correct amount of space is allocated, but it is the same as writing new int [2*3], to get 6 integers. You can write the allocation with a cast, like this: int *result = (int*)(new int[2][3]); or without a cast like this: int *result = new int[2*3]; I prefer the second way. Now you have to do a little arithmetic do map the memory into a 2 dimensional shape. You can use a little helper function like this: int& elementAt(int* mem, int Dim1Size, int index1, int index2) { return *(mem + index1 * Dim1Size + index2); } To treat memory as a 2 dimensional array, and find element [i][j], you need to provide the function with the start of the memory, the size of the first dimension, and the two indexes. The function calculates how far from the start of the memory the desired item is, and returns a reference to the item. The reference allows reading from and writing to the element. The sample program looks like this: using namespace std; int& elementAt(int *mem, int Dim1Size, int index1, int index2) { return *(mem + index1 * Dim1Size + index2); } int *myfunction(int inp[2][3]) { int i,j; int *result = new int[2*3]); for(i=0;i } int main() { int i, j; int arr1[2][3]={ {1,2,1}, {2,1,0} }; int* arr2; arr2=myfunction(arr1); for(i=0;i return 0; } I like to put my main at the bottom so that I don't have to declare all the functions up top, but that's just my habit. Notice that it is the int* that is being deleted, and because it is more that 1 element, we call delete with the square brackets. That is one way to do return an array from a function. There are many others. Perhaps a cleaner approach is not to return at all, but to have the caller pass in the result storage. Everything becomes simpler that way. Here is the sample program for that. using namespace std; void myfunction(int inp[2][3], int result[2][3]) { int i,j; for(i=0;i } int main() { int i, j; int arr1[2][3]={ {1,2,1}, {2,1,0} }; int arr2[2][3]; myfunction(arr1, arr2); for(i=0;i return 0; }
Miningco.com Visit the source
Related Q & A:
- What Is The Geometric Meaning Of Third Derivative Of A Function At A Point?Best solution by Mathematics
- How to pass a parameter to a function that is called on an event?Best solution by Stack Overflow
- How to call a function with parameter in a bash script?Best solution by tldp.org
- How to fire a function before and after automatically in a jquery plugin?Best solution by catchmyfame.com
- how to call a function in Python in another function?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.