利用指针数组输出二维数组

指针数组来输出二维数组 smart power

int (*pt)[4];
上述语句表示定义一个指针变量p,包含4个指向整形元素的一维数组,p表示该一维数组的首地址,可以将p看成是二维数组中的行指针,而p+i表示第i行的首地址,那么 *(pt+i) + j 就表示二维数组第i行第j列的元素地址,*(*(pt+i)+j)就表示第i行第j列的元素值。

代码中有一个地方不懂,已经注释出来了,欢迎评论大神指出。

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



int main()
{
    int test[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int *p,(*pt)[4],i,j;
    printf("The array is: \n");
    for(p = test[0]; p < &test[0][12]; p++)
    {

        if((p - test[0])%4 == 0)
            printf("\n%4d",*p);
        else
            printf("%4d",*p);

//        printf("\n\n%d,%d,%ld",p,test[0],p-test[0]); // 有个问题希望评论大神解决,为什么地址的差4表示1
    }
    printf("\nplease input the position like:i = , j = \n");
    scanf("i = %d, j = %d", &i, &j);
    pt = test;
    printf("\na[%d][%d] = %d",i,j, *(*(pt + i) + j));

    return 0;
}

==编程之路,你我相伴
在这里插入图片描述

发布了29 篇原创文章 · 获赞 5 · 访问量 688

猜你喜欢

转载自blog.csdn.net/ever_promise/article/details/104252653