1

I want to return a multidimensional matrix from a function.

So I know how to do that through a struct and dynamic allocating. But I'm not sure how to do that with a static multidimensional array.

#include <stdio.h>
static int **function(){
    static int array[5][10];
    return array;
}
int main(void){
    int** test;
    test = function();
    return 0;
}

The Gcc keep emiting this warning:

Warning: returning 'int (*)[10]' from a function with incompatible return type 'int **' [-Wincompatible-pointer-types] return array;

And I want to understand why?

2
  • 1
    The error message actually contains the actual type you're returning: "returning 'int (*)[10]' ". That's the return type you need. You might want to create a type-alias (using typedef) for it. Commented Jul 13, 2021 at 19:23
  • 1
    As for understanding what's happening, remember that arrays decays to pointers to their first element. That is, when you do return array; then array decay to &array[0]. And since array[0] is an array of ten int elements, the type of &array[0] is "pointer to array of ten int", or int (*)[10]. Commented Jul 13, 2021 at 19:25

3 Answers 3

2

In this return statement

return array;

the array designator array is converted to pointer to its first element of the type int ( * )[10]. But the function return type is int **. There is no implicit conversion from the type int ( * )[10] to the type int **. So the compiler will issue an error.

Pay attention to that the user of the function needs to know the sizes of the array.

So I advise you to declare the function like

static int ( *function( void ) )[5][10]
{
    static int array[5][10];
    return &array;
}

and then in main you can write

int ( *test )[5][10] = function();

dereferencing the pointer test you will get the array.

Here is a demonstrative program.

#include <stdio.h>

enum { M = 5, N = 10 };

static int ( *function( void ) )[M][N] 
{
    static int array[M][N] =
    {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
        { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
        { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
        { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
    };
    
    return &array;
}

int main(void) 
{
    int ( *test )[M][N] = function();
    
    printf( "The size of the array is %zu\n", sizeof( *test ) );
    
    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            printf( "%d ", ( *test )[i][j] );
        }
        putchar( '\n' );
    }
    
    return 0;
}

The program output is

The size of the array is 200
1 1 1 1 1 1 1 1 1 1 
2 2 2 2 2 2 2 2 2 2 
3 3 3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5 5

You can introduce an alias for the array type the following way to make the function declaration simpler

#include <stdio.h>

enum { M = 5, N = 10 };

typedef int Array[M][N];

static Array * function( void ) 
{
    static Array array =
    {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
        { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
        { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
        { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
    };
    
    return &array;
}

int main(void) 
{
    Array *test = function();
    
    printf( "The size of the array is %zu\n", sizeof( *test ) );
    
    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            printf( "%d ", ( *test )[i][j] );
        }
        putchar( '\n' );
    }
    
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is a big difference between a "pointer to a pointer" and a "pointer to an array". In your case, you need a "pointer to an array", which requires knowing the second dimension of the array. Try the following:

#include <stdio.h>

static int (*function(void))[10] {
    static int array[5][10];
    return array;
}

int main(void) {
    int (*test)[10];
    test = function();
    return 0;
}

Then from main you can access it as test[i][j].

1 Comment

You might want to show a version using a type-alias as well, as that will make thins very much simpler.
0

You can also use void * function.

static void *function(void) 
{
    static int array[5][10];
    return array;
}

int main(void) 
{
    int (*test)[10] = function();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.