| | |
Return multidimension array
![]() |
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
Hi all,
What should I write for function signature in order to return this 6X6 array without compiler error? I tried to use int* getGraph(), but it did not work.
Thanks for your help.
int[6][6] getGraph()
{
int graph[6][6] = {
{0, 5, 10, -1, -1, -1},
{-1, 0, -1, 2, -1, -1},
{-1, -1, 0, -1, 10, -1},
{-1, -1, -1, 0, 1, -1},
{-1, -1, -1, -1, 0, 10},
{-1, -1, -1, -1, -1, 0} };
return graph;
}
What should I write for function signature in order to return this 6X6 array without compiler error? I tried to use int* getGraph(), but it did not work.
Thanks for your help.
int[6][6] getGraph()
{
int graph[6][6] = {
{0, 5, 10, -1, -1, -1},
{-1, 0, -1, 2, -1, -1},
{-1, -1, 0, -1, 10, -1},
{-1, -1, -1, 0, 1, -1},
{-1, -1, -1, -1, 0, 10},
{-1, -1, -1, -1, -1, 0} };
return graph;
}
When you return from your proposed function, the local data goes out of scope. So it is not useful to do it this way. Could you describe what it is you would like to accomplish?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
To Kalachylde: It does not perform what we are expected to because of two reasons: it will return integer instead of array of integer, and return graph[6][6] means creating one new array of integer with size 6X6 without initialization.
To Dave Sinkula: I think although the local data goes out of scope, we still get the correct result because the local data is copied to a temporary memory, which is returns to the caller before the local data going out of scope.
Now, I think the least sufficient way is using class (wrapper the multi-array), and returning its's object. However, if anyone has a greater idea, please share with us.
By the way, thanks for both your inputs.
To Dave Sinkula: I think although the local data goes out of scope, we still get the correct result because the local data is copied to a temporary memory, which is returns to the caller before the local data going out of scope.
Now, I think the least sufficient way is using class (wrapper the multi-array), and returning its's object. However, if anyone has a greater idea, please share with us.
By the way, thanks for both your inputs.
Unless there is some language extension with which I am not yet familiar, you cannot return arrays (I'm more sure of this for C). An array name, when used in an expression, decays to a pointer to the first element. The function would then be returning a pointer to local data that is now out of scope. It may appear to be correct, or it may not -- both are within the realm of undefined behavior.
If the array is within a class (again I'm going with more familiarity with C's structs) -- yes, you can return an object.
Another idea: pass a pointer to the start and copy the desired contents there.
If the array is within a class (again I'm going with more familiarity with C's structs) -- yes, you can return an object.
Another idea: pass a pointer to the start and copy the desired contents there.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> void getGraph(int my_graph[6][6]) { static const int default_graph[6][6] = { { 0, 5, 10, -1, -1, -1}, {-1, 0, -1, 2, -1, -1}, {-1, -1, 0, -1, 10, -1}, {-1, -1, -1, 0, 1, -1}, {-1, -1, -1, -1, 0, 10}, {-1, -1, -1, -1, -1, 0}, }; memcpy(my_graph, default_graph, sizeof default_graph); } int main(void) { int graph[6][6]; getGraph(graph); /* ... */ return 0; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
Yes. Absolutely, you are right about local data goes out of scope, but the value is always correct since the pointer is going out of scope, but the memory, which pointer pointed to is still there unless you explicitly delete pointer, then that memory location will contain garbage.
I really appreciate for your help.
I really appreciate for your help.
•
•
•
•
Originally Posted by tlee
Yes. Absolutely, you are right about local data goes out of scope, but the value is always correct since the pointer is going out of scope, but the memory, which pointer pointed to is still there unless you explicitly delete pointer, then that memory location will contain garbage.
Let's say you have an array local to a function. This data is created on the stack. You call this function and return a pointer to the first element of it -- you are pointing somewhere on the stack.
Then you call another function that also has local variables. This function also uses the stack, and it tramples your data. And the pointer returned by the first function is still pointing to the same place.
This is just one possible example of your assertion being incorrect. So "always correct" is not true.
[edit]Since you can't return an array, this is the closest simulation I could devise.
C Syntax (Toggle Plain Text)
#include <stdio.h> int *getGraph(void) { int init[6][6] = { { 0, 5, 10, -1, -1, -1}, {-1, 0, -1, 2, -1, -1}, {-1, -1, 0, -1, 10, -1}, {-1, -1, -1, 0, 1, -1}, {-1, -1, -1, -1, 0, 10}, {-1, -1, -1, -1, -1, 0}, }; int i, j; puts("getGraph:"); for (i = 0; i < 6; ++i) { for (j = 0; j < 6; ++j) { printf("%2d ", init[i][j]); } putchar('\n'); } return init; } void printGraph(const int g[6][6]) { int i, j; puts("printGraph:"); for (i = 0; i < 6; ++i) { for (j = 0; j < 6; ++j) { printf("%2d ", g[i][j]); } putchar('\n'); } } int main(void) { int (*graph)[6] = getGraph(); printGraph(graph); return 0; } /* my output getGraph: 0 5 10 -1 -1 -1 -1 0 -1 2 -1 -1 -1 -1 0 -1 10 -1 -1 -1 -1 0 1 -1 -1 -1 -1 -1 0 10 -1 -1 -1 -1 -1 0 printGraph: 0 5 10 -1 -1 -1 -1 0 -1 2 -1 4223327 -1 1245004 10 1244919 4223327 4223339 0 11 -1 -1 1245028 4209022 4208532 4224044 4223339 1245044 1245056 4198704 4223339 4223339 0 4223088 0 1245068 */
Last edited by Dave Sinkula; Jun 15th, 2004 at 7:39 pm. Reason: Added code sample.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: c vs h header files...
- Next Thread: Please help with algorithm
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






