指针与数组

#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];  //不能这样赋值,为什么呢????????

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; //故这句不应该注销
}
}
}

}


#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]

上面的例子一能打印出来全部数据,但是额外地打印了未知数据,至于为什么,没有搞明白。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值