LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 753|回复: 8

没办法输出准确

[复制链接]
发表于 2004-8-26 15:29:29 | 显示全部楼层 |阅读模式
  1. [color=darkblue]
  2. #include<stdio.h>
  3.                                                                                                                
  4. struct person {
  5.   char *name;
  6.   char *addr;
  7.   float score;
  8. };
  9.                                                                                                                
  10. struct person inc (struct person );
  11.                                                                                                                
  12. main()
  13. {
  14.   struct person ss;
  15.   static  struct person shellon={"shelloning","longhorn",555.555};
  16.   printf("Before calling ... \n");
  17.   printf("name is %s\taddr is %s\tscore is %f\n",shellon.name,shellon.addr,shellon.score);
  18.                                                                                                                
  19.   ss=inc(shellon);
  20.                                                                                                                
  21.   printf("After calling ... \n");
  22.   printf("name is %s\taddr is %s\tscore is %f\n",ss.name,ss.addr,ss.score);
  23. }
  24.                                                                                                                
  25. struct person inc(struct person ii)
  26. {
  27.   ii.name="shellon";
  28.   ii.addr="nothing";
  29.   ii.score=888.888;
  30.   return (ii);
  31. }
  32. [/color]
复制代码

  1. Before calling ...
  2. name is shelloning      addr is longhorn        score is [color=crimson]555.554993[/color]
  3. After calling ...
  4. name is shellon addr is nothing score is 888.888000
复制代码



这里的score没办法输出准确,该如何办 ?
 楼主| 发表于 2004-8-26 15:35:15 | 显示全部楼层
第二个问题是:after calling后,后面输出时,却没有\t,程序本身已经有\t的了,如果再加\t的话,共两个才可以输出跟前面的一样

也就说说22行换成下面那样才可以输出带有1个\t的效果

  1. printf("name is %s[color=firebrick]\t\t[/color]addr is %s[color=firebrick]\t\t[/color]score is %f\n",ss.name,ss.addr,ss.score)
复制代码


输出才是

Before calling ...
name is shelloning      addr is longhorn        score is 555.554993
After calling ...
name is shellon         addr is nothing         score is 888.888000


为什么会这样 ?
发表于 2004-8-26 22:11:10 | 显示全部楼层
score是float型的,
就这个样子
发表于 2004-8-27 09:32:16 | 显示全部楼层
我们平时作用的是十进制数,转换成二进制时是不能完全精确表示的。这就是出现这个问题的原因。
发表于 2004-8-30 04:09:22 | 显示全部楼层
人家问的是为什么要两个\t才能和前面一个\t一样显示啊
btw:我从来没见过象这样struct传值的写法,不知道编译器对这样的参数传递如何处理的,谁给说说
发表于 2004-8-30 08:57:41 | 显示全部楼层
一个用struct传参数的小程序

  1. typedef struct{
  2.    int arr[10];
  3. }data_t;

  4. void func(data_t data)
  5. {
  6.     return;
  7. }

  8. int main()
  9. {
  10.     data_t d;

  11.     func(d);
  12.     return 0;
  13. }
复制代码

用gcc-3.2.2编译:
  1.     .file   "main.c"
  2.     .text
  3. .globl func
  4.     .type   func,@function
  5. func:
  6.     pushl   %ebp
  7.     movl    %esp, %ebp
  8.     leave
  9.     ret
  10. .Lfe1:
  11.     .size   func,.Lfe1-func
  12. .globl main
  13.     .type   main,@function
  14. main:
  15.     pushl   %ebp
  16.     movl    %esp, %ebp
  17.     pushl   %edi
  18.     pushl   %esi
  19.     subl    $48, %esp          /* 在堆栈中分配局部变量d的空间 */
  20.     andl    $-16, %esp
  21.     movl    $0, %eax
  22.     subl    %eax, %esp
  23.     subl    $8, %esp
  24.     subl    $40, %esp        /* 在堆栈中分配参数的空间 */
  25.     movl    %esp, %edi
  26.     leal    -56(%ebp), %esi
  27.     cld
  28.     movl    $10, %eax
  29.     movl    %eax, %ecx
  30.     rep
  31.     movsl           /* 把d的内容拷贝到参数空间 */
  32.     call    func
  33.     addl    $48, %esp
  34.     movl    $0, %eax
  35.     leal    -8(%ebp), %esp
  36.     popl    %esi
  37.     popl    %edi
  38.     leave
  39.     ret
  40. .Lfe2:
  41.     .size   main,.Lfe2-main
  42.     .ident  "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
复制代码

显然用struct传参数是一个很浪费堆栈的做法。

我记得在Windows下用VC6编译的结果与此非常不同,并且当struct空间比较小和比较大的时候处理方法也有所不同,有兴趣的兄弟可以试一下。
千禧湖的青蛙 该用户已被删除
发表于 2004-8-31 14:31:08 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2004-8-31 15:52:25 | 显示全部楼层
明白了,呵呵
 楼主| 发表于 2004-9-1 06:55:22 | 显示全部楼层
真的太谢谢你们了,,,,,,我理解了,,,,谢谢你们
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表