Originally Posted by
ssharish2005
I'm trying to return a 2D array from a function. And i was trying to form a function declaration. Thats wasn't from your code.
ssharish
C cannot return arrays from functions... it doesn't know how. Inside any function except the one where the array is declared, C does not know how big it is, all size information is lost and without that, it cannot copy the array out to the calling function.
That is why you mostly see the array's pointer and size passed in the top of the function as parameters.
Run this small program for an example of why you should never try to return an array from a function...
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; }