免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: studentstep
打印 上一主题 下一主题

这里为何传指针不可以呢 ? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-11-16 15:46 |只看该作者
up

论坛徽章:
0
12 [报告]
发表于 2005-11-16 15:51 |只看该作者

  1. #include<stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>

  4. struct STRU
  5. {
  6.                 int stru_i_count ;
  7.                 char stru_c ;
  8. } ;


  9. void change_struct(struct STRU * stru , int c , int count)
  10. {
  11.                 stru->stru_c = c ;
  12.                 stru->stru_i_count = count ;
  13. }

  14. int main(int argc , char * argv[])
  15. {

  16.                 struct STRU * st1 ;
  17.                 st1 = (struct STRU *)malloc(sizeof(struct STRU) * 1) ;
  18.                 memset(st1 , 0 , sizeof(struct STRU)) ;

  19.                 change_struct(st1 , 200 , 100) ;

  20.                 printf("%c , %dn" , st1->stru_c , st1->stru_i_count) ;
  21.                 free(st1) ;

  22. }
复制代码


把char c改为int c 便不能的到正确的结果。
struct tm必须传指针的指针可能是因为tm结构中包含了int变量

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
13 [报告]
发表于 2005-11-16 15:56 |只看该作者
好好理解一下 型参 和实参就明白了.

论坛徽章:
0
14 [报告]
发表于 2005-11-17 08:56 |只看该作者
我知道传值不可以修改实参,传址可以修改实参,就如:


  1. struct STRU
  2. {
  3.                 int stru_i_count ;
  4.                 char stru_c ;
  5. } ;


  6. void change_struct(struct STRU * stru , char c , int count  int test)
  7. {
  8.                 stru->stru_c = c ; // 传址,可以被改变
  9.                 stru->stru_i_count = count ;
  10.                 test = 100 ; // 传值,不能被改变
  11. }

  12. int main(int argc , char * argv[])
  13. {

  14.                 struct STRU * st1 ;
  15.                 int i = 0 ;
  16.                 st1 = (struct STRU *)malloc(sizeof(struct STRU) * 1) ;
  17.                 memset(st1 , 0 , sizeof(struct STRU)) ;

  18.                 change_struct(st1 , 'A' , 100 , i) ;

  19.                 printf("%c , %dni=%dn" , st1->stru_c , st1->stru_i_count , i) ;
  20.                 free(st1) ;
  21.                
  22. }
复制代码


但就为何在 :



  1. int lib_sec2tm(long sec , struct tm * m)  //
  2. {
  3.     time_t timep ;
  4.     timep = (time_t)sec ;        
  5.     m = localtime(&timep);                      //
  6.     return 0 ;
  7. }

  8. int get_sec2tm(long isec)
  9. {
  10.                 struct tm * t = NULL ;
  11.                 char * str_time = NULL ;

  12.                 t = (struct tm *)malloc(sizeof(struct tm)) ;
  13.                 memset(t , 0 , sizeof(struct tm)) ;
  14.                 str_time = (char *)malloc(sizeof(char) * 100 + 1) ;
  15.                 memset(str_time , 0 , 100) ;

  16.                 lib_sec2tm(isec , t) ;                  //
  17.                 strftime(str_time,100,"%Y-%m-%d %H:%M:%S , %sn",t);   
  18.                 printf("Sec is %ld , convert to time %sn", isec , str_time);         // 打印出 1900-11-00 ...        
  19.                                                                                                                                                                                                                                                                                 // 应该是 2005-11-13 ...               
  20.                 free(t) ;
  21.                 free(str_time) ;
  22.                
  23. }

  24. 不能修改 t 的值呢 ? 两个程序中传的都是结构的地址啊 ,  为什么呢 ?
复制代码

论坛徽章:
0
15 [报告]
发表于 2005-11-17 09:07 |只看该作者
原帖由 studentstep 于 2005-11-17 08:56 发表
我知道传值不可以修改实参,传址可以修改实参,就如:

[code]
change_struct(st1 , 'A' , 100 , i) ;


str1和其他参数一样,都是传了值,都是局部变量。mq110说得对,理解了型参实参的含义就好了

[ 本帖最后由 openX 于 2005-11-17 10:13 编辑 ]

论坛徽章:
0
16 [报告]
发表于 2005-11-17 09:13 |只看该作者
为何 m = localtime(&timep);   localtime(&timep) 不会把它的返回放在 m 中呢 ?

论坛徽章:
0
17 [报告]
发表于 2005-11-17 13:26 |只看该作者
结贴
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP