• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Return 2D array from a function

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
JessyEzzy
Asked:
JessyEzzy
  • 7
  • 3
1 Solution
 
AxterCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
AxterCommented:
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
 
JessyEzzyAuthor Commented:
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
 
AxterCommented:
>>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
 
AxterCommented:
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
 
JessyEzzyAuthor Commented:
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
 
AxterCommented:
>>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
 
JessyEzzyAuthor Commented:
Ok got it thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now