# Thread: Returning Array from Function

1. ## Returning Array from Function

can any one please explain me this code :-

Code:
```#include<stdio.h>
#include<conio.h>

#define ROW 3
#define COL 4

int main()

{
int i,j;
int(*c)[ROW][COL];
int (*fun3())[ROW][COL];

c=fun3();
printf("Array c[][] in main():\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
printf("%d\n",(*c)[i][j]);
printf("\n");
}
_getch();
return 0;
}

int (*fun3())[ROW][COL]
{
static int c[ROW][COL]={
6,3,9,1,
2,1,5,7,
4,1,1,6

};

int i,j;
printf("Aray c[][] in fun3():\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%d",c[i][j]);
printf("\n");
}
}
return (int(*)[ROW][COL])c;
}```

i am not getting
1.
Code:
`int (*fun3())[ROW][COL];`

2. working of this :-

Code:
``` for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
printf("%d\n",(*c)[i][j]); ```
3.
Code:
` return (int(*)[ROW][COL])c;`
please explain and suggest some site/blog/book where i can read on this

2. You're probably mixing up types, although I haven't looked closely. A typedef is usually a handy helper to avoid confusion, particularly if you are playing with functions returning pointers to non-trivial things.
Code:
```#include <stdio.h>

#define ROW 3
#define COL 4

typedef int YourArray[ROW][COL];

int main()
{
YourArray *fun3();
YourArray *c = fun3();
/* print out array as in your code */
return 0;

}

YourArray *fun3()
{
static int c[ROW][COL]={
6,3,9,1,
2,1,5,7,
4,1,1,6

};
return &c;
}```

3. First C cannot return an array from a function... it doesn't know how. The best you can get is a pointer which may or may not be valid after the function returns... Here's a little test program you can run to see why you should not do this...

Code:
```#include <stdio.h>

int* MyFunction(int a, int b, int c)
{  static int array[3];
array[0] = a;
array[1] = b;
array[2] = c;
return array;  } // return a pointer.

int main (void)
{ int *a1, *a2;  // int pointers

printf("calling a1 = MyFunction(10,20,30);\t");
a1 = MyFunction(10,20,30);
printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);

printf("calling a2 = MyFunction(100,200,300);\t");
a2 = MyFunction(100,200,300);
printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);

printf("\nLooks good, except...\t");
printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);

getchar();
return 0; }```

4. Originally Posted by tarunjain07
can any one please explain me this code :-

Code:
```#include<stdio.h>
#include<conio.h>

#define ROW 3
#define COL 4

int main()

{
int i,j;
int(*c)[ROW][COL];
int (*fun3())[ROW][COL];

c=fun3();
printf("Array c[][] in main():\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
printf("%d\n",(*c)[i][j]);
printf("\n");
}
_getch();
return 0;
}

int (*fun3())[ROW][COL]
{
static int c[ROW][COL]={
6,3,9,1,
2,1,5,7,
4,1,1,6

};

int i,j;
printf("Aray c[][] in fun3():\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%d",c[i][j]);
printf("\n");
}
}
return (int(*)[ROW][COL])c;
}```
i am not getting
1.
Code:
`int (*fun3())[ROW][COL];`

Hi this is function declaration. This meaning function(fun3) returning an pointer to an two dimentional array of width(row) and height(col)

2. working of this :-

Code:
``` for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
printf("%d\n",(*c)[i][j]); ```
here you are going to print the values in two dimensional array. Actually c is pointer to two dimensional array. so you can get those values by (*c)[i][j].

3.
Code:
` return (int(*)[ROW][COL])c;`

Here the function returning the pointer(c) to an two dimensional array. Because we already declared this function prototype in main fucntion.

please explain and suggest some site/blog/book where i can read on this

5. Originally Posted by tarunjain07
i am not getting
...
Code:
` return (int(*)[ROW][COL])c;`
That cryptic cast is pointless, when just returning a pointer to "c" is enough:
Code:
`return &c;`