How to return 2-dimension array in a function
-
-
In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'
int* getA()
{
int a[2];
return a;
}int** getB()
{
int b[3][2];return b;
}
Why bother? Returning a temporary would trouble you anyway. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'
int* getA()
{
int a[2];
return a;
}int** getB()
{
int b[3][2];return b;
}
akira32 wrote:
In getA, I can get 1-dimension array.
The first thing here is that you're not "get[ing] 1-dimension array." you're returning the address of the first element of where a 1 dimensional array was before the function returned. Depending on what you do with the pointer after the function returns you could crash your program or even leave it with a big security hole. The bad news is that you can't return an array of any sort directly from a function. The slightly better news is that you can return a structure containing an array, e.g:
struct array_wrapper
{
int data[10];
};struct array_wrapper get_array()
{
struct array_wrapper a;
a.data[5] = 100;
return a;
}Quick caveat: I don't program in C anymore so I may have got the syntax a bit wrong. Cheers, Ash Edited to fix slightly knackered code formatting
modified on Friday, May 14, 2010 5:54 PM
-
In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'
int* getA()
{
int a[2];
return a;
}int** getB()
{
int b[3][2];return b;
}
-
In getA, I can get 1-dimension array. But in getB, it will appear the error message as below: Error 2 error C2440: 'return' : cannot convert from 'int [3][2]' to 'int **'
int* getA()
{
int a[2];
return a;
}int** getB()
{
int b[3][2];return b;
}
You shouldn't be returning an array, the best thing is to accept a pointer to an array allocated by the caller (plus dimensions) and then fill it. That way you'll avoid the problem of who has to do the cleanup of the memory. For example this function will fill the array with random values:
int foo (int* dstArr, int m, int n)
{
for (int i=0;i
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal -
Both these calls leave you with memory leaks. If you want to allocate arrays to be returned from a function you should use one of the memory allocation functions,
malloc()
ornew
.It's time for a new signature.
Hi, Sorry to sound picky but it's important for C programmers to understand these things... The original functions the original author posted didn't leak anything (well, the one that would have compiled anyway) - all the memory allocated for his arrays on the stack were cleaned up when the functions returned. The problem he would have caused was actually the reverse of a leak: continuing to access memory that's been marked as inaccessible for some reason. This is usually called a "dangling pointer" as it's pointing somewhere it shouldn't. In this case it's pointing to a block of memory on the stack and writing to it could cause all sorts of fun and merriment. Cheers, Ash
-
You shouldn't be returning an array, the best thing is to accept a pointer to an array allocated by the caller (plus dimensions) and then fill it. That way you'll avoid the problem of who has to do the cleanup of the memory. For example this function will fill the array with random values:
int foo (int* dstArr, int m, int n)
{
for (int i=0;i
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal