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、测试结果是对的。