東西,如下
#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