C语言:通过指针来引用多维数组元素


C语言:通过指针来引用多维数组元素

一、通过指针来引用二维数组格式:char_template[i][j]等于*(*(char_template+i)+j)

二、通过指针来引用三维数组格式:char_template[i][j][y]等于*(*(*(char_template+i)+j)+y)。

三、实例学习
1、测试代码:cpoint.c
#include <stdio.h>  
#define FINGER_COUNT 2
#define FEATURE_SIZE   3
#define ENROLL_COUNT 4

unsigned char char_template_2[FINGER_COUNT][ENROLL_COUNT]= {{{2}}};
unsigned int  uint_template_2[FINGER_COUNT][ENROLL_COUNT]= {{{5}}};

unsigned char char_template[FINGER_COUNT][ENROLL_COUNT][FEATURE_SIZE] = {{{2}}};
unsigned int  uint_template[FINGER_COUNT][ENROLL_COUNT][FEATURE_SIZE] = {{{5}}};
void main()  
{   unsigned char *cp,*ip;
    int i,j,y,z;
	cp=char_template[0][0];
	ip=uint_template[0][0];
	printf("char_template_2's start address=%d\n",char_template_2); 
	printf("uint_template_2's start address=%d\n",uint_template_2); 
	printf("char_template's   start address=%d\n",char_template); 
	printf("uint_template's   start address=%d\n",uint_template); 
	z=255;
	for(i=0;i<FINGER_COUNT;i++)
    {
		for(j=0;j<ENROLL_COUNT;j++)
		{
		  
		    char_template_2[i][j]=z;
            uint_template_2[i][j]=z+10000;
			z--;
		}
		
	}
	z=0;
    for(i=0;i<FINGER_COUNT;i++)
    {
		for(j=0;j<ENROLL_COUNT;j++)
		{
		    	printf("char_template_2[%d][%d]=%d,char_template_2's address is %d,*(*(*(char_template_2+i)+j)+y)=%d  ",i,j,char_template_2[i][j],*(char_template_2+i)+j,*(*(char_template_2+i)+j)); 
		        printf("uint_template_2[%d][%d]=%d,uint_template_2's address is %d,*(*(*(uint_template_2+i)+j)+y)=%d\n",i,j,uint_template_2[i][j],*(uint_template_2+i)+j,*(*(uint_template_2+i)+j)); 

		}
		
	}
	
	printf("\n\n\n"); 
	z=0;
    for(i=0;i<FINGER_COUNT;i++)
    {
		for(j=0;j<ENROLL_COUNT;j++)
		{
		  for(y=0;y<FEATURE_SIZE;y++)
		  {
		    char_template[i][j][y]=z;
            uint_template[i][j][y]=z+10000;
			z++;
		  }
		}
		
	}
	z=0;
    for(i=0;i<FINGER_COUNT;i++)
    {
		for(j=0;j<ENROLL_COUNT;j++)
		{
		  for(y=0;y<FEATURE_SIZE;y++)             
		  {
		    	printf("char_template[%d][%d][%d]=%d,char_template's address is %d,*(*(*(char_template+i)+j)+y)=%d  ",i,j,y,char_template[i][j][y],*(*(char_template+i)+j)+y,*(*(*(char_template+i)+j)+y)); 
		        printf("uint_template[%d][%d][%d]=%d,uint_template's address is %d,*(*(*(uint_template+i)+j)+y)=%d\n",i,j,y,uint_template[i][j][y],*(*(uint_template+i)+j)+y,*(*(*(uint_template+i)+j)+y)); 
		  }
		}
		
	}
	printf("\n"); 
}  
     2、编译&执行

    3、测试结果是对的。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值