How can i return an entire array from a function into the main function after i call it?In C program?
-
-
Answer:
In general, it's a pain in C. C needs to have "compile time" knowledge about the dimensions of a matrix or else it cannot automatically calculate the addresses given the indices and simply doesn't know what to do. So the question comes down to whether or not your matrices are of fixed size. If you imagine the first index as a row and the second index as a column, C needs to at least know the number of columns. You can create a varying number of rows, then. But not a varying number of columns. If you want both to be varying, then you need to return the matrix as a simple vector and do all the indexing calculations using explicit formulas in your code. (This is part of the reason why some people have created general-purpose matrix libraries for C. These will provide structures that include the necessary information to manage the matrices.) Assume you are just returning fixed sized matrices where both dimensions are always known at compile time. Then: int (*my5x10( void ))[5][10] { return (int (*)[5][10]) malloc( 50 * sizeof(int) ); } will allocate the matrix on the heap and return a pointer to it. Note the somewhat arcane syntax. But it reads "a function called my5x10 that takes void as parameters and returns a pointer to a 5x10 array of integers." You will have to later free that matrix, elsewhere, since it was allocated on the heap. You cannot write this, safely: int (*my5x10( void ))[5][10] { int myarray[5][10]; return &myarray; } The local array won't be valid as soon as the function returns (under some operating systems anyway), so the matrix made that way is useless to the calling routine. That's why you need the heap. (Or else some static lifetime matrix.) So you could say: int (*my5x10( void ))[5][10] { static int myarray[5][10]; return &myarray; } You can return structures from functions. So you could put the matrix into a structure and then return it. But as the above shows already, you cannot return the matrix without a structure wrapper or as a pointer to the matrix. Never the matrix itself. The syntax doesn't support it. So: typedef struct { int arr[5][10]; } matrix5x10_t; matrix5x10_t myfunc( void ) { stupid_t x; return x; } Would work. This forces the compiler to pass in a hidden pointer (usually) from the caller that is a pointer to its receiving structure and the function uses that, either directly or else at the end by performing a copy operation from the local variable into the caller's. (Optimization may eliminate that copy step.) If you need variable sized matrices, both dimensions, then you need to do your own calculations and instead use an array and break it up into rows using your own formula to do so.
Jonathan at Yahoo! Answers Visit the source
Other answers
In general, it's a pain in C. C needs to have "compile time" knowledge about the dimensions of a matrix or else it cannot automatically calculate the addresses given the indices and simply doesn't know what to do. So the question comes down to whether or not your matrices are of fixed size. If you imagine the first index as a row and the second index as a column, C needs to at least know the number of columns. You can create a varying number of rows, then. But not a varying number of columns. If you want both to be varying, then you need to return the matrix as a simple vector and do all the indexing calculations using explicit formulas in your code. (This is part of the reason why some people have created general-purpose matrix libraries for C. These will provide structures that include the necessary information to manage the matrices.) Assume you are just returning fixed sized matrices where both dimensions are always known at compile time. Then: int (*my5x10( void ))[5][10] { return (int (*)[5][10]) malloc( 50 * sizeof(int) ); } will allocate the matrix on the heap and return a pointer to it. Note the somewhat arcane syntax. But it reads "a function called my5x10 that takes void as parameters and returns a pointer to a 5x10 array of integers." You will have to later free that matrix, elsewhere, since it was allocated on the heap. You cannot write this, safely: int (*my5x10( void ))[5][10] { int myarray[5][10]; return &myarray; } The local array won't be valid as soon as the function returns (under some operating systems anyway), so the matrix made that way is useless to the calling routine. That's why you need the heap. (Or else some static lifetime matrix.) So you could say: int (*my5x10( void ))[5][10] { static int myarray[5][10]; return &myarray; } You can return structures from functions. So you could put the matrix into a structure and then return it. But as the above shows already, you cannot return the matrix without a structure wrapper or as a pointer to the matrix. Never the matrix itself. The syntax doesn't support it. So: typedef struct { int arr[5][10]; } matrix5x10_t; matrix5x10_t myfunc( void ) { stupid_t x; return x; } Would work. This forces the compiler to pass in a hidden pointer (usually) from the caller that is a pointer to its receiving structure and the function uses that, either directly or else at the end by performing a copy operation from the local variable into the caller's. (Optimization may eliminate that copy step.) If you need variable sized matrices, both dimensions, then you need to do your own calculations and instead use an array and break it up into rows using your own formula to do so.
Jonathan
arrays are passed to functions as address/pointer and they are returned the same way an array declared locally inside the function can not be returned unless it is declared as static. You are actually return the address of the array int **func(....) { static int result[666][999]; ... code here ... return result; } if you do this without it being static you return garbage that is written over
Huh?
Here is an example to play and test: #include <iostream> using namespace std; int** devueveArray2(int dimX, int dimY) { int **arr = new int*[dimX]; for(int i=0; i<dimX; ++i) { arr[i] = new int[dimY]; } for(int i=0; i<dimX; ++i) for(int j=0; j<dimY; ++j) arr[i][j] = dimY*i+j; return arr; } int main() { int dimX, dimY; cout << "ingresa las filas y columnas: "; cin >> dimX >> dimY; int **a = devueveArray2(dimX, dimY); for(int i=0; i<dimX; ++i) { for(int j=0; j<dimY; ++j) { cout << a[i][j] << "\t"; } cout << "\n"; } for(int i=0; i<dimX; ++i) delete[] a[i]; delete[] a; return 0; }
Alejandroc
arrays are passed to functions as address/pointer and they are returned the same way an array declared locally inside the function can not be returned unless it is declared as static. You are actually return the address of the array int **func(....) { static int result[666][999]; ... code here ... return result; } if you do this without it being static you return garbage that is written over
Huh?
Here is an example to play and test: #include <iostream> using namespace std; int** devueveArray2(int dimX, int dimY) { int **arr = new int*[dimX]; for(int i=0; i<dimX; ++i) { arr[i] = new int[dimY]; } for(int i=0; i<dimX; ++i) for(int j=0; j<dimY; ++j) arr[i][j] = dimY*i+j; return arr; } int main() { int dimX, dimY; cout << "ingresa las filas y columnas: "; cin >> dimX >> dimY; int **a = devueveArray2(dimX, dimY); for(int i=0; i<dimX; ++i) { for(int j=0; j<dimY; ++j) { cout << a[i][j] << "\t"; } cout << "\n"; } for(int i=0; i<dimX; ++i) delete[] a[i]; delete[] a; return 0; }
Alejandroc
Related Q & A:
- Can I return a car back to a dealer?Best solution by Yahoo! Answers
- How can I delete my entire inbox on my blackberry curve?Best solution by Yahoo! Answers
- How can I correct sent emails from showing a wrong return address?Best solution by Yahoo! Answers
- How can I make a phone call to a cruise ship?Best solution by traveltips.usatoday.com
- How can I return a product online?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.