| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 497 人关注过本帖
标题:指针问题
只看楼主 加入收藏
lc5491137
Rank: 2
等 级:论坛游民
帖 子:37
专家分:70
注 册:2012-3-5
结帖率:100%
收藏
已结贴  问题点数:13 回复次数:5 
指针问题
#include<stdio.h>
#define MONTHS 12
#define YEARS 5
int main(void)
{
     double rain[YEARS][MONTHS]=
     {
          {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
          {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
          {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
          {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
          {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
     };
     int year,month;
     double subtot,total;
     double (*ptr)[12];
     ptr=rain;
     
     printf(" YEAR   RAINFALL (inches) \n");
     for(year = 0,total = 0;year<YEARS;year++)
     {
          for(month=0,subtot=0;month<MONTHS;month++)
          {
              subtot+=(**ptr);
               (*(ptr+year))++;
          }
          printf("%5d   %15.1f\n",2000+year,subtot);
          total+=subtot;
     }
    printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);
     printf("MONTHLY AVERAGES: \n\n");
     printf(" Jan Feb Mar Apr May Jun Jul Aug Sep OCT ");
     printf(" Nov Dec\n");
     
     for(month=0;month<MONTHS;month++)
     {
          for(year=0,subtot=0;year<YEARS;year++)
          {
               subtot+=(**ptr);
               (*(ptr+year)+month)++;
          }
          printf("%4.1f",subtot/YEARS);
     }
     printf("\n");
     return 0;
}
为什么(*(ptr+year))++;和这个(*(ptr+year)+month)++;都是错的

[ 本帖最后由 lc5491137 于 2012-4-17 16:09 编辑 ]
搜索更多相关主题的帖子: double total include 
2012-04-17 14:27
heyufeng493
Rank: 2
等 级:论坛游民
帖 子:3
专家分:13
注 册:2012-4-11
收藏
得分:13 
这是改完的,你自己对照着看吧,祝你好运。
#include<stdio.h>
 #define MONTHS 12
 #define YEARS 5
 int main(void)
 {
      double rain[YEARS][MONTHS]=
      {
           {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
           {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
           {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
           {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
           {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
      };
      int year,month;
      double subtot,total;
      double (*ptr)[12];
      double *a[5];
      
      printf(" YEAR   RAINFALL (inches) \n");
      for(year = 0,total = 0;year<YEARS;year++)
      {
          ptr=rain;
           for(month=0,subtot=0;month<MONTHS;month++)
           {
               subtot+=*(*(ptr+year)+month);
           }
           printf("%5d   %15.1f\n",2000+year,subtot);
           total+=subtot;
      }
     printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);
      printf("MONTHLY AVERAGES: \n\n");
      printf(" Jan   Feb   Mar   Apr   May  Jun   Jul   Aug   Sep   OCT ");
      printf("  Nov  Dec\n");
      
      for(month=0,subtot=0;month<MONTHS;month++)
      {
        
           for(year=0,subtot=0;year<YEARS;year++)
           {
               a[year]=rain[year];
               subtot+=*(*(a+year)+month);
           }
           printf("%4.1f  ",subtot/YEARS);
      }
      printf("\n");
      return 0;
 }
  

[ 本帖最后由 heyufeng493 于 2012-4-17 16:39 编辑 ]
2012-04-17 15:24
lc5491137
Rank: 2
等 级:论坛游民
帖 子:37
专家分:70
注 册:2012-3-5
收藏
得分:0 
回复 2楼 heyufeng493
同学:这里指的是多维数组,只是这里
subtot+=**ptr;
(*(ptr+year))++和下面for循环中同样的有问题而已
2012-04-17 16:14
heyufeng493
Rank: 2
等 级:论坛游民
帖 子:3
专家分:13
注 册:2012-4-11
收藏
得分:0 
回复 3楼 lc5491137
2L是改完的,我运行了,结果也正确,你还有什么要说的。
2012-04-17 16:35
lc5491137
Rank: 2
等 级:论坛游民
帖 子:37
专家分:70
注 册:2012-3-5
收藏
得分:0 
回复 3楼 lc5491137
我早知道正确答案了,我想知道的是为什么
subtot += *(*(ptr + year) + month);不能被替换成
subtot+=(**ptr);
(*(ptr+year))++;

并且subtot += *(*(ptr + year) + month);不能替换成
subtot+=(**ptr);
(*(ptr+year)+month)++;

[ 本帖最后由 lc5491137 于 2012-4-17 17:51 编辑 ]
2012-04-17 17:45
heyufeng493
Rank: 2
等 级:论坛游民
帖 子:3
专家分:13
注 册:2012-4-11
收藏
得分:0 
回复 5楼 lc5491137
开始定义的double (*ptr)[12];这是数组指针,基类型是“行”,而且他只是一个指针,不是12个指针,而且每次移动必须移动一行,subtotal+=(*ptr)这个没问题,但是(*(ptr+year))++这里就不行了,你可能是想让他每次移动8个字节(double),但是数组指针自增就 表示移动一行。

2012-04-18 17:28
快速回复:指针问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014813 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved