Returning Array from Function

This is a discussion on Returning Array from Function within the C Programming forums, part of the General Programming Boards category; can any one please explain me this code :- Code: #include<stdio.h> #include<conio.h> #define ROW 3 #define COL 4 int main() ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    19

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    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;
    }
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Quote Originally Posted by tarunjain07 View Post
    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
    Last edited by nkrao123@gmail.; 09-03-2011 at 08:03 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by tarunjain07 View Post
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a 2D array from a function
    By sauwen in forum C Programming
    Replies: 5
    Last Post: 11-19-2010, 04:51 PM
  2. c++ -- returning an array from function
    By p4plus2 in forum C++ Programming
    Replies: 25
    Last Post: 08-18-2008, 01:48 PM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Returning an array from a function
    By cjschw in forum C++ Programming
    Replies: 10
    Last Post: 07-20-2003, 12:03 AM
  5. returning an array from a function
    By BubbleBoy in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 11:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21