Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Return 2D array from a function

Posted on 2006-05-16
10
Medium Priority
?
213 Views
Last Modified: 2010-04-01
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
Comment
Question by:JessyEzzy
  • 7
  • 3
10 Comments
 
LVL 30

Expert Comment

by:Axter
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

by:Axter
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

by:Axter
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Axter
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

by:JessyEzzy
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

by:Axter
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

by:
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

by:JessyEzzy
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

by:Axter
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

by:JessyEzzy
ID: 16724095
Ok got it thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question