Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Return 2D array from a function

Posted on 2006-05-16
Medium Priority
213 Views
I have this function in which I want to declare 6 two dimensions array, this function takes an integer value on which I decide which one of the 6 arrays to return this is how I imagine how the function could be, but I don't know what to put for the return type, I have put int but ofcourse this causes an error, how would I do something like that?

int sbox(int x)
{
int s1[4][2] = { {1,2,3,4}, {5,6,7,8} };
int s2[4][2] = { {9,10,11,12}, {13,14,15,16} };
.............
.............
till int s6[4][2]

if(x == 1)
return s1
else if(x ==2)
return s2
..........
...........
}

Thanks.
0
Question by:JessyEzzy
• 7
• 3

LVL 30

Expert Comment

ID: 16694847
Hi JessyEzzy,
> how would I do something like that?

It's better to have the value set to an input parameter instead of trying to return a two dimensional array.

If you still want to return a 2D array, you can create a typdef for the 2D array, and then return a pointer of that type.

Make sure you're not returning data that will go out of scope when the function exits.

I also recommend you use vector instead of a C style array.

David Maisonave (Axter)
Cheers!
0

LVL 30

Expert Comment

ID: 16694855
Here's an example on how to return an intrinsic C-Style array.

typedef const bool (&ID_TYPE)[2];

ID_TYPE func(bool v1, bool v2)
{
static bool x[2]={false, false};
x[0] = v1;
x[1] = v2;
return x;
}

int main(int argc, char* argv[])
{
cout << ((func(true, true)[0])?"true":"false") << endl;
cout << ((func(true, true)[1])?"true":"false") << endl;

cout << ((func(false, false)[0])?"true":"false") << endl;
cout << ((func(false, false)[1])?"true":"false") << endl;

system("pause");
return 0;
}
0

LVL 30

Expert Comment

ID: 16694862
Here's an example function that returns a filled array using data from an input array.

int (*func14(int (*y)[3], int s))[3]
{
int (*x)[3] = (int(*)[3])malloc(sizeof(int)*3*s);
for (int i = 0;i < s;++i)
{
for (int ii = 0;ii < 3;++ii)
{
x[i][ii] = y[i][ii];
}
}
return x;
}
0

LVL 30

Expert Comment

ID: 16694889
For your requirement, you can use the following typedef:
typedef int (&MY_INT_4X2_TYPE)[4][2];

MY_INT_4X2_TYPE sbox(int x)
{
......
0

LVL 3

Author Comment

ID: 16694970
Hi Axter,
thanks for the quick replies, but unfortunately I didn't get any of your solutions :(

>>It's better to have the value set to an input parameter instead of trying to return a two dimensional array.

What do u mean by that?

There is a solution that I tried but it didn't work, I made this

void sbox(int x, int arr[4][2])
{
int s1[4][2] = { {1,2,3,4}, {5,6,7,8} };
int s2[4][2] = { {9,10,11,12}, {13,14,15,16} };
.............
.............
till int s6[4][2]

if(x == 1)
arr = s1
else if(x ==2)
arr = s2
..........
...........
}

then in the main, I declared an array and passed it to the function, I thought that since arrays are by default passed by reference, I thought this could work

int arr[4][2];
sbox(1,arr);

but when I tried printing the values of arr I got rubbish values something like
-83787952
-86635477
0

LVL 30

Expert Comment

ID: 16695036
>>There is a solution that I tried but it didn't work, I made this

Try the following:

typedef int (INT_4X2_TYPE)[4][2];

void sbox(int x, INT_4X2_TYPE &arr)

0

LVL 30

Accepted Solution

Axter earned 500 total points
ID: 16695049
The above method passes a reference to the array.
You also need to modify your function logic.

>>  if(x == 1)
>>     arr = s1
>>   else if(x ==2)
>>     arr = s2

The above code is not going to work.  You need to iterate through each item in the array, and assign the value.

for(int i = 0;i < 4;++i)
for(int ii = 0;ii < 2;++ii)
arr[i][ii] = s1[i][ii];
0

LVL 3

Author Comment

ID: 16718820
I tried your solution it's working now, thanks a lot.
But I'm interested to know why this code is not working, what's wrong in doing this?

>>  if(x == 1)
>>     arr = s1
>>   else if(x ==2)
>>     arr = s2
0

LVL 30

Expert Comment

ID: 16719655
>>what's wrong in doing this?

Because arr points to the first element in the array.
In C and in C++, you can not copy an array by asigning it to another array.
That's why the following doesn't work:

char data1[33] = "Hello World";
char data2[33] = "Goodby World";

data2 = data1;

The above code will give you a compile error.
0

LVL 3

Author Comment

ID: 16724095
Ok got it thanks.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more ā¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a ā¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anā¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatā¦
###### Suggested Courses
Course of the Month20 days, 16 hours left to enroll