当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 10 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : linux下编译不通过,windows下编译通过,但是结果错误(已解决)
帖子发表于 : 2012-01-04 16:51 
头像

注册: 2007-11-02 13:46
帖子: 970
送出感谢: 0 次
接收感谢: 1
代码:
#include <stdio.h>
#define YEARS 5
#define MONTHS 12

int main(void)
{
    float rain_tot;
   
    const float rain[YEARS][MONTHS]={
      {4.3,4.3,4.3,3.2,2.0,2.0,1.2,0.5,3.5,4.3,5.3,5.6},
      {8.5,8.2,1.2,1.6,2.4,0.0,0.2,0.9,0.3,1.4,5.2,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.2,0.0,0.6,1.7,4.3},
      {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,1.3,1.7,2.6,5.2}
   };
   
    printf(" YEAR RAINFALL (inches):\n");
    rain_tot=rain_year(rain,YEARS);
    printf("The number returned from rain_tot() is %.1f.\n",rain_tot);
    //年平均降水量
    printf("The yearly average is %.1f inches.\n",rain_tot/YEARS);
   
    //格式化输出每个月份的平均降水量的表头:
    printf("The monthly averages are:\n");
    printf("Yan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
   
    rain_month(rain,YEARS);//月平均降水量
   
    return 0;
}

//这里需要返回数值的,使用float类型就会报错,为什么只能使用void类型?
void rain_year(float array[][MONTHS],int n)
{
    int year,month;
    float total,subtot;
   
    //年降水总量
    for(year=0,total=0;year<n;year++){
        for(month=0,subtot=0;month<MONTHS;month++)
            subtot+=*(*(array+year)+month);
        printf("%5d\t%.1f\n",2000+year,subtot);
        total+=subtot;
    }
    printf("The total rainfall is %.1f inches.\n",total);//这里的total值为200.1
   
    return total;//这里的返回值怎么是36.0呢?
}

void rain_month(float array[][MONTHS],int n)
{
    int year,month;
    float subtot;
   
    //月平均降水量
    for(month=0;month<MONTHS;month++){
        for(year=0,subtot=0;year<n;year++)
            subtot+=*(*(array+year)+month);
        printf("%-4.1f",subtot/n);
        }
    printf("\n");
}

linux下编译不通过
代码:
##将rain_year()的类型改为float
[[email protected] test]$ gcc 10-11.c -o rain
10-11.c:39: error: conflicting types for 'rain_year'
10-11.c:23: error: previous implicit declaration of 'rain_year' was here
10-11.c:56: error: conflicting types for 'rain_month'
10-11.c:32: error: previous implicit declaration of 'rain_month' was here
10-11.c:68:5: warning: no newline at end of file

windows下给出警告但是编译通过,但是在rain_tot()内部的total值跟函数返回值total不一致。在windows编译的时候:
1、rain_year()需要返回total的值,但是如果将函数类型定义为float,就会报错;改为void就通过,这是为什么呢?
2、函数rain_year()中,在第44行输出的total值是200.1,第46行中return total这一步返回数值是36.0,这是为什么呢?
代码:
E:\C Programming\PrimerPlus\test>gcc 10-11.c -o rain
10-11.c:38:6: warning: conflicting types for 'rain_year' [enabled by default]
10-11.c:23:14: note: previous implicit declaration of 'rain_year' was here
10-11.c: In function 'rain_year':
10-11.c:52:5: warning: 'return' with a value, in function returning void [enable
d by default]
10-11.c: At top level:
10-11.c:55:6: warning: conflicting types for 'rain_month' [enabled by default]
10-11.c:32:5: note: previous implicit declaration of 'rain_month' was here
E:\C Programming\PrimerPlus\test>rain.exe
 YEAR RAINFALL (inches):
 2000   40.5
 2001   37.2
 2002   49.8
 2003   38.0
 2004   34.6
The total rainfall is 200.1 inches.
The number returned from rain_tot() is 36.0.
The yearly average is 7.2 inches.
The monthly averages are:
Yan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
7.3 7.3 4.9 3.0 2.3 0.8 0.4 0.4 1.2 2.1 4.2 6.2
E:\C Programming\PrimerPlus\test>


_________________
明察,慎思,笃行


最后由 renxinzhi 编辑于 2012-01-06 11:27,总共编辑了 1 次

页首
 用户资料  
 
2 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 16:56 
头像

注册: 2011-08-17 12:54
帖子: 287
送出感谢: 0 次
接收感谢: 4
原型放主函数前面。。


页首
 用户资料  
 
3 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 17:26 
头像

注册: 2009-07-25 1:57
帖子: 701
送出感谢: 5
接收感谢: 13
当gcc和vc编译结果不一致的时候,参考gcc的,更贴近c/c++标准。(vc能通过,是因为它默认就假定使用者是个那啥。。。)。


_________________
https://github.com/tangboyun
http://tangboyun.is-programmer.com/
提问的智慧————Eric Steven Raymond
回答的智慧————Andrew Clarke
吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。
急急急标题什么的,最讨厌了!
急急复急急,急急何其多,我生待急急,万事急急急。


页首
 用户资料  
 
4 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 17:26 
头像

注册: 2007-11-02 13:46
帖子: 970
送出感谢: 0 次
接收感谢: 1
dryland718 写道:
原型放主函数前面。。

谢谢。
由于数组rain被const进行了数据保护,所以在函数调用数组rain的时候会出现提示:warning: passing arg 1 of `rain_year' from incompatible pointer type,那么怎么能不修改数组rain的const属性,又使得不再出现这个警告呢?
还有,将函数原型放置到main()函数之前,rain_tot()的返回值就变得正常了,请教这是为什么。
再次感谢。


_________________
明察,慎思,笃行


最后由 renxinzhi 编辑于 2012-01-04 17:29,总共编辑了 1 次

页首
 用户资料  
 
5 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 17:28 
头像

注册: 2007-11-02 13:46
帖子: 970
送出感谢: 0 次
接收感谢: 1
tangboyun 写道:
当gcc和vc编译结果不一致的时候,参考gcc的,更贴近c/c++标准。(vc能通过,是因为它默认就假定使用者是个那啥。。。)。

你没有注意到在linux下和windows下,我使用的编译器都是gcc。


_________________
明察,慎思,笃行


页首
 用户资料  
 
6 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 17:41 
头像

注册: 2011-08-17 12:54
帖子: 287
送出感谢: 0 次
接收感谢: 4
renxinzhi 写道:
dryland718 写道:
原型放主函数前面。。

谢谢。
由于数组rain被const进行了数据保护,所以在函数调用数组rain的时候会出现提示:warning: passing arg 1 of `rain_year' from incompatible pointer type,那么怎么能不修改数组rain的const属性,又使得不再出现这个警告呢?
还有,将函数原型放置到main()函数之前,rain_tot()的返回值就变得正常了,请教这是为什么。
再次感谢。

第一个,参数用const修饰,比如void foo(const int a[]);
第二个,c语言常识,在函数调用之前必须保证出现过函数原型(标准规定c语言变量声明放在函数最开始也是相似的原理,具体原因可以百度,主要是栈分配问题)。


页首
 用户资料  
 
7 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 17:47 
头像

注册: 2007-11-02 13:46
帖子: 970
送出感谢: 0 次
接收感谢: 1
dryland718 写道:
renxinzhi 写道:
dryland718 写道:
原型放主函数前面。。

谢谢。
由于数组rain被const进行了数据保护,所以在函数调用数组rain的时候会出现提示:warning: passing arg 1 of `rain_year' from incompatible pointer type,那么怎么能不修改数组rain的const属性,又使得不再出现这个警告呢?
还有,将函数原型放置到main()函数之前,rain_tot()的返回值就变得正常了,请教这是为什么。
再次感谢。

第一个,参数用const修饰,比如void foo(const int a[]);
第二个,c语言常识,在函数调用之前必须保证出现过函数原型(标准规定c语言变量声明放在函数最开始也是相似的原理,具体原因可以百度,主要是栈分配问题)。

十分感谢。


_________________
明察,慎思,笃行


页首
 用户资料  
 
8 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 20:49 
头像

注册: 2009-07-25 1:57
帖子: 701
送出感谢: 5
接收感谢: 13
renxinzhi 写道:
tangboyun 写道:
当gcc和vc编译结果不一致的时候,参考gcc的,更贴近c/c++标准。(vc能通过,是因为它默认就假定使用者是个那啥。。。)。

你没有注意到在linux下和windows下,我使用的编译器都是gcc。

之前我确实没看到你win下也用的gcc,不过也能解释的通. 不同平台下的与之兼容的“默认”编译器是不一样的,linux下所有的c/c++编译器都尽可能的与gcc一致,在win下则是与vc一致, 也就是事实标准.
我猜你的这个差异也是因为以上原因,至少intel的编译器是这么做的,也就是linux下与gcc靠拢(但还是少一些宏定义),win下与vc一致. 可以试试看vc能通过否.


_________________
https://github.com/tangboyun
http://tangboyun.is-programmer.com/
提问的智慧————Eric Steven Raymond
回答的智慧————Andrew Clarke
吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。
急急急标题什么的,最讨厌了!
急急复急急,急急何其多,我生待急急,万事急急急。


页首
 用户资料  
 
9 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-04 21:58 
头像

注册: 2008-07-24 11:33
帖子: 4793
地址: 郑州
送出感谢: 4
接收感谢: 92
Windows下的gcc是mingw吧,那个版本低。


_________________
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~


页首
 用户资料  
 
10 楼 
 文章标题 : Re: linux下编译不通过,windows下编译通过,但是结果错误
帖子发表于 : 2012-01-05 13:28 
头像

注册: 2007-11-02 13:46
帖子: 970
送出感谢: 0 次
接收感谢: 1
cuihao 写道:
Windows下的gcc是mingw吧,那个版本低。

我使用的是gfortran4.7中自带的gcc,4.7的gcc,不低了吧。
代码:
E:\C Programming\PrimerPlus\test>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=d:/gfortran/bin/../libexec/gcc/i586-pc-mingw32/4.7.0/lto-wra
pper.exe
Target: i586-pc-mingw32
Configured with: ../gcc-trunk/configure --prefix=/mingw --enable-languages=c,for
tran --with-gmp=/home/brad/gfortran/dependencies --disable-werror --enable-threa
ds --disable-nls --build=i586-pc-mingw32 --enable-libgomp --enable-shared --disa
ble-win32-registry --with-dwarf2 --disable-sjlj-exceptions --enable-lto
Thread model: win32
gcc version 4.7.0 20111212 (experimental) [trunk revision 182257] (GCC)

E:\C Programming\PrimerPlus\test>gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=d:/gfortran/bin/../libexec/gcc/i586-pc-mingw32/4.7.0/lto-wra
pper.exe
Target: i586-pc-mingw32
Configured with: ../gcc-trunk/configure --prefix=/mingw --enable-languages=c,for
tran --with-gmp=/home/brad/gfortran/dependencies --disable-werror --enable-threa
ds --disable-nls --build=i586-pc-mingw32 --enable-libgomp --enable-shared --disa
ble-win32-registry --with-dwarf2 --disable-sjlj-exceptions --enable-lto
Thread model: win32
gcc version 4.7.0 20111212 (experimental) [trunk revision 182257] (GCC)


_________________
明察,慎思,笃行


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 10 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 0 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译