且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何返回一个二维数组

更新时间:2021-11-08 18:47:56

#include <stdio.h>
#include <stdlib.h>

#define COUNT 3

typedef int (*Mystype)[COUNT];

Mystype yourfunction(int n)
{
    Mystype p;
    int i = 0, j = 0;
    if (NULL==(p = (Mystype)malloc(n*COUNT*sizeof(int))))
    {
        printf("OVERFLOW!\n");
        exit(0);
    }
    for (; i<n; i++)
    {
        for (j=0; j<COUNT; j++)
        {
            p[i][j] = j+1;    // p[i][j] == *(*(p+i)+j);
        }
    }
    return p;
}

void print_array(Mystype arry, int n)
{
    int i = 0, j = 0;
    for (; i<n; i++)
    {
        for (j=0; j<COUNT; j++)
        {
            printf("%d ",arry[i][j]); // arry[i][j] == *(*(arry+i)+j);
        }
        printf("\n");
    }
}

void main()
{
    int n = 4;
    Mystype a = yourfunction(n);
    print_array(a,n);
}