Guyz I Don't want to write another array function using VOID I wanna do it using pointers for the extra grades...... can ny one give me a clude or just6 a sample code snippet on how to return a 2-D array ?? I have to fing sum of upper half of left diagonal of a 2-D int array.

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by atish00

Pointing to a 2D array is just like pointing to a 1D array, all the pointer does it point to the very first element of the array. Heres an example that shows how you can point to multi-dimensional arrays by using pointers.

``````#include<iostream>
using namespace std;

void DisplayNums1D(int *nums, int length1) {
for (int i = 0; i < length1; ++i) {
cout << nums[i] << '\n';
}
}

void DisplayNums2D(int *nums, int length1, int length2) {
for (int i = 0; i < length1; ++i) {
for (int j = 0; j < length2; ++j) {
cout << nums[i*length2 + j] << '\n';
}
}
}

int main() {
int _1D[5]    = {1, 2, 3, 4, 5};

int _2D[5][2] = {1, 2,
3, 4,
5, 6,
7, 8,
9, 10};

cout << "\n1D:\n\n";
DisplayNums1D(&_1D[0], 5);

cout << "\n2D:\n\n";
DisplayNums2D(&_2D[0][0], 5, 2);

cin.ignore();

return 0;
}``````

To understand how it works you have to think of a 2D array as a 1D array, imagine you have this 2D array with dimensions [5][2]:

``````[B]index	1	2[/B]
[B]1[/B]	1	2
[B]2[/B]	3	4
[B]3[/B]	5	6
[B]4[/B]	7	8
[B]5[/B]	9	10``````

You can imagine it as a 1D array when stored in memory, like this.

``````[B]index[/B]
1	2	3	4	5	6	7	8	9	10``````

and by pointing to the sixth element in the array, its the same as pointing to [1][3] in the first 2D array.

Hope this helps you understand.

Presumably you're already passing the array in as a parameter, so returning it is a little pointless (The original will be modified anyway, since arrays are always passed by-pointer to a function).

Arrays aren't copyable, so they must be returned by-pointer. The syntax gets a bit messy, so you ought to use a typedef to simplify things a little

``````const int num_elems = 10;

typedef int (*array2d)[num_elems];

array2d function (int arr[][num_elems])
{
return arr;
}``````

Without the typedef you'd end up with brackets all over the place, like this

``````int (*func (int arr[][num_elems]) ) [num_elems]
{
return arr;
}``````

Just a word of warning - if you do insist on returning arrays from a function, make sure that the array has been declared outside of the function from which its returning, otherwise the end of the function will cause the array to be destroyed, and your program will most likely crash or display some other kind of undefined behaviour.

``````//Very bad! - Do not ever do this!
int (*function () ) [num_elems]
{
int my_array[5][num_elems];
return my_array;
}``````

cpp Syntax (Toggle Plain Text)

1.
//Very bad! - Do not ever do this!
2.
int (*function () ) [num_elems]
3.
{
4.
int my_array[5][num_elems];
5.
return my_array;
6.
}

How to read this returned value ??

BTW thanx for the help.

I need to Input an array and then return it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.