东西,如下
#include <stdio.h>
#define N 7
#define M 5
const int (* getPrint(void))[M]
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main(void)
{
const int (*ptr)[M];
int i, j;
ptr = getPrint();
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++) printf("%d ", ptr[i][j]);
printf("\n");
}
return 0;
}
或者你可以使用一个typedef。例如
#include <stdio.h>
#define N 7
#define M 5
typedef const int (*ArrayPtr)[M];
ArrayPtr getPrint(void)
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main(void)
{
ArrayPtr ptr;
int i, j;
ptr = getPrint();
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++) printf("%d ", ptr[i][j]);
printf("\n");
}
return 0;
}
无,因为* *有未定义的行为*(返回一个指向局部变量的指针。) – juanchopanza 2014-09-20 12:39:57
循环?而你的代码有UB。 – 2014-09-20 12:46:26
'int **'与int [] []不一样'' – d3L 2014-09-20 13:00:35