#include <stdio.h>
int main(void){
int calendar[12][31];
int (*month)[31];
int (*month0)[31];
int (*month1)[31];
month = &calendar[11];
month0 = calendar[11];
month1 = &calendar[11][0]; //调试可知,上面三种写法都是一样的
for(month = calendar; month < &calendar[12];month++)
{
int *day;
int *day0;
int *day1;
//day0 = &(*month)[30];
//day1 = &month[31]; //不能这样赋值,为什么呢????????
for(day = month,i = 1;day<&month[31],i< 32;day++,i++)
*day = i;
}
for(month = calendar; month < &calendar[12];month++)
{
int *day;
static int count = 0; //注意static int count = 0只初始化一次,第二、三、...次进入循环时,该语句不会在执行
for(day = month;day<&month[31];day++)
{
printf(" %d",*day);
count++;
if(count == 31)
{
printf("\n");
count = 0; //故这句不应该注销
}
}
}
}
#include <stdio.h>
int main(void)
{
int calendar[12][31];
int (*month)[31];
int (*month0)[31];
int (*month1)[31];
month = &calendar[11];
month0 = calendar[11];
month1 = &calendar[11][0]; //调试可知,上面三种写法都是一样的,但是实际上还是有些不同的,编译器会打印警告。
//因为calendar[11] 和 &calendar[11][0] 都是表示 calendar[11][0]元素的地址,与month的类型不匹配
for(month = calendar; month < &calendar[12];month++)
{
int *day;
int i;
for(day = *month,i = 1;day<&(*month)[31],i< 32;day++,i++)
*day = i;
}
for(month = calendar; month < &calendar[12];month++)
{
int *day;
static int count = 0; //注意static int count = 0只初始化一次,第二、三、...次进入循环时,该语句不会在执行
for(day = *month;day<&(*month)[31];day++)
{
printf(" %d",*day);
count++;
if(count == 31)
{
printf("\n");
count = 0; //故这句不应该注销
}
}
}
}
我们知道a[i] 实际上是*(p+i)的简化形式 。例如:
int calendar[12][31];
int *p;
int i;
p = calendar[4] ;指针p指向了数组calendar[4]中下标为0的元素。
i = calendar[4][7]; 可以写成 i = *(calendar[4] +7);进一步还可以写为:i = * (*(calendar+4)+7);
故:
*(*(calendar + month) + day) = 0;
故:&(*month)[31]
应该化简为&(*(month+0))[31] 即&month[0][31]
上面的例子一能打印出来全部数据,但是额外地打印了未知数据,至于为什么,没有搞明白。