int C[10][20];

int * mmatrix (int A[ 10 ][ 5 ], int B[ 5 ][ 20 ])
{
	
	int i, j, k;

for(i = 0; i < 10; ++i)
	for(j = 0; j < 20; ++j) {
		for(k = 0; k < 5; ++k)
			C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
		}

	return C;
}

Why can not my compiler convert to this? What is the problem over here?

Thanks

Not entirely sure what you are going for here, but you have a 2-D array and a function that returns a pointer to an integer, so at the very least, you need to dereference that 2D array at least once.

return *C;

This will at least compile.

Not entirely sure what you are going for here, but you have a 2-D array and a function that returns a pointer to an integer, so at the very least, you need to dereference that 2D array at least once.

return *C;

This will at least compile.

An array's name is its beginning address, isn't it? So it is a type of int *?

That's true. It compiled but I want to learn how can I return C successfully? What would I write as a return type?

An array's name is its beginning address, isn't it? So it is a type of int *?

That's true. It compiled but I want to learn how can I return C successfully? What would I write as a return type?

It's a 2-D array, so it's more like an int**, not an int*. A pointer to the beginning of a 3 dimensional array would be an int***. A pointer to a 4-dimensional array would be an int****.

So if you are writing a function that returns a pointer to the front of a 2-D array, the return type would likely be int**. It gets a little complicated returning pointers to a STATIC 2-D array, but returning a pointer to a DYNAMIC 2-D array could be accomplished like this:

int ** mmatrix (int A[ 10 ][ 5 ], int B[ 5 ][ 20 ])
{
    int** C = new int*[10];
    for (int a = 0; a < 10; a++)
        C[a] = new int[20];

    int i, j, k;

    for(i = 0; i < 10; ++i)
	    for(j = 0; j < 20; ++j) {
		    for(k = 0; k < 5; ++k)
			    C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
		}



    int* ptrToOneDimensionalArray = C[0];
    int** ptrToTwoDimensionalArray = C;
	return ptrToTwoDimensionalArray;
}

This, on the other hand (static array), won't compile:

int C[10][20];

int ** mmatrix (int A[ 10 ][ 5 ], int B[ 5 ][ 20 ])
{
 //   int** C = new int*[10];
 //   for (int a = 0; a < 10; a++)
 //       C[a] = new int[20];

	int i, j, k;

    for(i = 0; i < 10; ++i)
	    for(j = 0; j < 20; ++j) {
		    for(k = 0; k < 5; ++k)
			    C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
		}



    int* ptrToOneDimensionalArray = C[0];
    int** ptrToTwoDimensionalArray = C;
	return ptrToTwoDimensionalArray;
}

This below will compile. You are returning the address of the first element (C[0][0])of the array below (int*, not int**).

int C[10][20];

int * mmatrix (int A[ 10 ][ 5 ], int B[ 5 ][ 20 ])
{
 //   int** C = new int*[10];
 //   for (int a = 0; a < 10; a++)
 //       C[a] = new int[20];

	int i, j, k;

    for(i = 0; i < 10; ++i)
	    for(j = 0; j < 20; ++j) {
		    for(k = 0; k < 5; ++k)
			    C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
		}

    int* ptrToOneDimensionalArray = C[0];
    int* ptrToFirstElementOfTwoDimensionalArray = &(C[0][0]);
	return ptrToFirstElementOfTwoDimensionalArray;
}

Please note that the top example leads to a memory leak since I reserve memory in the function, but never free that memory. But I was concentrating more on the return types.

im not sure why you are even returning C in this function becase you already have it defined in the global namespace. anything you do inside the function will automaticly change C to what you are doing. also for your information even though an array is a type of pointer you still will need to derefernce the array to pass it as a pointer

> It's a 2-D array, so it's more like an int**, not an int*.
Only in as much as you can use [x][y] notation to step through them.
A pointer to a 2D array is NOT the same as a pointer to a pointer.
http://c-faq.com/aryptr/pass2dary.html

An example of actual pointers to 2D arrays, being passed and returned.

#include <stdio.h>

/* the syntax for returning a pointer to a 2D array is hairy! */
int (*hard( int (*a)[20]) )[20] {
    static int result[10][20];
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            result[r][c] = a[r][c];
        }
    }
    return result;
}

/* a suitable typedef makes it so much easier on the eye */
/* The typedef adds nothing NEW here, hard() and easy() are fully */
/* interchangeable when it comes to using them */
typedef int (*twod)[20];
twod easy( twod a ) {
    static int result[10][20];
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            result[r][c] = a[r][c];
        }
    }
    return result;
}

/* Yet another equivalent way of declaring the input array parameter */
void fill ( int a[10][20] ) {
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            a[r][c] = r * c;
        }
    }
}
void show ( int a[10][20] ) {
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            printf( "%3d ", a[r][c] );
        }
        printf("\n");
    }
    printf("---\n");
}


int main ( ) {
    int test[10][20];
    fill(test);
    {
        int (*r1)[20] = hard(test);
        show(r1);
    }
    {
        twod r2 = easy(test);
        show(r2);
    }
    return 0;
}
Comments
Thanks man but something more to read on this will improve the underestandibility of everyone who dig this in future
Hairy indeed.
Good link.

Thank you salem. I understood. But I want to ask you one thing more.

what is the difference between them?

int (*hard( int (*a)[20]) )[20] {
    static int result[10][20];

    return result;
}
int *(hard( int (*a)[20]) )[20] {
    static int result[10][20];

    return result;
}

The first is a pointer to an array.
The second is an array of pointers (or invalid, I dunno, you compile it and find out).

The ( ) are immensely important in getting these kinds of type declarations correct.

This article has been dead for over six months. Start a new discussion instead.