如何从 c 中的函数 return 多维静态数组
How to return a multidimensional static array from a function in c
我想return一个函数的多维矩阵。
所以我知道如何通过结构和动态分配来做到这一点。但是我不确定如何使用静态多维数组来做到这一点。
#include <stdio.h>
static int **function(){
static int array[5][10];
return array;
}
int main(void){
int** test;
test = function();
return 0;
}
Gcc 不断发出此警告:
Warning: returning 'int (*)[10]' from a function with
incompatible return type 'int **' [-Wincompatible-pointer-types]
return array;
我想知道为什么?
“指向指针的指针”和“指向数组的指针”有很大区别。在您的情况下,您需要一个“指向数组的指针”,这需要知道数组的第二维。请尝试以下操作:
#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;
}
然后您可以从 main
访问它作为 test[i][j]
。
在这个return声明中
return array;
数组指示符 array
被转换为指向类型 int ( * )[10]
的第一个元素的指针。但是函数return类型是int **
。没有从类型 int ( * )[10]
到类型 int **
的隐式转换。所以编译器会报错。
注意函数的使用者需要知道数组的大小。
所以我建议你像这样声明函数
static int ( *function( void ) )[5][10]
{
static int array[5][10];
return &array;
}
然后在 main 中你可以写
int ( *test )[5][10] = function();
解引用指针 test
你将得到数组。
这是一个演示程序。
#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 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
您可以通过以下方式为数组类型引入别名,使函数声明更简单
#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;
}
您也可以使用void *
函数。
static void *function(void)
{
static int array[5][10];
return array;
}
int main(void)
{
int (*test)[10] = function();
}
我想return一个函数的多维矩阵。
所以我知道如何通过结构和动态分配来做到这一点。但是我不确定如何使用静态多维数组来做到这一点。
#include <stdio.h>
static int **function(){
static int array[5][10];
return array;
}
int main(void){
int** test;
test = function();
return 0;
}
Gcc 不断发出此警告:
Warning: returning 'int (*)[10]' from a function with incompatible return type 'int **' [-Wincompatible-pointer-types] return array;
我想知道为什么?
“指向指针的指针”和“指向数组的指针”有很大区别。在您的情况下,您需要一个“指向数组的指针”,这需要知道数组的第二维。请尝试以下操作:
#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;
}
然后您可以从 main
访问它作为 test[i][j]
。
在这个return声明中
return array;
数组指示符 array
被转换为指向类型 int ( * )[10]
的第一个元素的指针。但是函数return类型是int **
。没有从类型 int ( * )[10]
到类型 int **
的隐式转换。所以编译器会报错。
注意函数的使用者需要知道数组的大小。
所以我建议你像这样声明函数
static int ( *function( void ) )[5][10]
{
static int array[5][10];
return &array;
}
然后在 main 中你可以写
int ( *test )[5][10] = function();
解引用指针 test
你将得到数组。
这是一个演示程序。
#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 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
您可以通过以下方式为数组类型引入别名,使函数声明更简单
#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;
}
您也可以使用void *
函数。
static void *function(void)
{
static int array[5][10];
return array;
}
int main(void)
{
int (*test)[10] = function();
}