943,949 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7178
  • C RSS
Jun 7th, 2004
0

Return multidimension array

Expand Post »
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;
}
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
Jun 8th, 2004
0

Re: Return multidimension array

I'm unsure of this but I don't think "int [6][6]" will work. Did you try "int getGraph()" and "return graph[6][6]"?
Reputation Points: 12
Solved Threads: 1
Newbie Poster
kalachylde is offline Offline
9 posts
since Jun 2004
Jun 8th, 2004
0

Re: Return multidimension array

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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 15th, 2004
0

Re: Return multidimension array

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.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
Jun 15th, 2004
0

Re: Return multidimension array

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.
  1. #include <stdio.h>
  2. #include <string.h>
  3. void getGraph(int my_graph[6][6])
  4. {
  5. static const int default_graph[6][6] =
  6. {
  7. { 0, 5, 10, -1, -1, -1},
  8. {-1, 0, -1, 2, -1, -1},
  9. {-1, -1, 0, -1, 10, -1},
  10. {-1, -1, -1, 0, 1, -1},
  11. {-1, -1, -1, -1, 0, 10},
  12. {-1, -1, -1, -1, -1, 0},
  13. };
  14. memcpy(my_graph, default_graph, sizeof default_graph);
  15. }
  16. int main(void)
  17. {
  18. int graph[6][6];
  19. getGraph(graph);
  20. /* ... */
  21. return 0;
  22. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 15th, 2004
0

Re: Return multidimension array

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.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
Jun 15th, 2004
0

Re: Return multidimension array

Quote 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.
No. Undefined behavior is, well, undefined behavior.

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.
  1. #include <stdio.h>
  2. int *getGraph(void)
  3. {
  4. int init[6][6] =
  5. {
  6. { 0, 5, 10, -1, -1, -1},
  7. {-1, 0, -1, 2, -1, -1},
  8. {-1, -1, 0, -1, 10, -1},
  9. {-1, -1, -1, 0, 1, -1},
  10. {-1, -1, -1, -1, 0, 10},
  11. {-1, -1, -1, -1, -1, 0},
  12. };
  13. int i, j;
  14. puts("getGraph:");
  15. for (i = 0; i < 6; ++i)
  16. {
  17. for (j = 0; j < 6; ++j)
  18. {
  19. printf("%2d ", init[i][j]);
  20. }
  21. putchar('\n');
  22. }
  23. return init;
  24. }
  25. void printGraph(const int g[6][6])
  26. {
  27. int i, j;
  28. puts("printGraph:");
  29. for (i = 0; i < 6; ++i)
  30. {
  31. for (j = 0; j < 6; ++j)
  32. {
  33. printf("%2d ", g[i][j]);
  34. }
  35. putchar('\n');
  36. }
  37. }
  38. int main(void)
  39. {
  40. int (*graph)[6] = getGraph();
  41. printGraph(graph);
  42. return 0;
  43. }
  44. /* my output
  45. getGraph:
  46.  0 5 10 -1 -1 -1
  47. -1 0 -1 2 -1 -1
  48. -1 -1 0 -1 10 -1
  49. -1 -1 -1 0 1 -1
  50. -1 -1 -1 -1 0 10
  51. -1 -1 -1 -1 -1 0
  52. printGraph:
  53.  0 5 10 -1 -1 -1
  54. -1 0 -1 2 -1 4223327
  55. -1 1245004 10 1244919 4223327 4223339
  56.  0 11 -1 -1 1245028 4209022
  57. 4208532 4224044 4223339 1245044 1245056 4198704
  58. 4223339 4223339 0 4223088 0 1245068
  59. */
Last edited by Dave Sinkula; Jun 15th, 2004 at 7:39 pm. Reason: Added code sample.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 15th, 2004
0

Re: Return multidimension array

Sorry! I take back my words. In the above case it is wrong since the above array is not allocated in the heap(unless using new operator), so you are 100% correct. (I messed up with Java since array is object).
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: c vs h header files...
Next Thread in C Forum Timeline: Please help with algorithm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC