c语言编程-----指向二维数组的指针

c中如何返回一个指向二维数组的指针

#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);
}

 

posted @ 2017-03-13 15:12  Anita-ff  阅读(351)  评论(0编辑  收藏  举报