码迷,mamicode.com
首页 > 其他好文 > 详细

第二次作业补充

时间:2017-03-31 18:05:59      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:细节   close   循环条件   指针   images   运行   std   bsp   style   

一、课上的代码

技术分享
 1 #include<stdio.h>
 2 int main()
 3 {    void funstr(char str);
 4      char str[12]={"hello world!"};
 5      str[0]=H;
 6      funstr(str);
 7      return 0;
 8 } 
 9 void funstr(char str)
10 {
11     int t;
12     printf("%s\n",str);
13     while(t!=\0)
14     {
15         printf("%c\n",str);
16         t++;
17     }
18 }
课上的代码

显然,课上的代码是不对的。编译器显示正确,但却未能运行。

技术分享

二、课下的工作

有许多细节部位致错,课下通过查书p76页表3.8,明确了%c是字符输出函数,%s是字符串输出函数。

p260页可得知,需要定义一个指针变量去指向数组。且该代码没while的循环变量定义初始条件

数组没有定义至足够长度也是另一个方面。循环条件(为t)及输出有\n也出现了错误,所以致错。

三、正确的代码

技术分享
 1 #include<stdio.h>
 2 int main()
 3 {    void funstr(char str[12]);
 4      char str[12]={"hello world"};
 5      char *a=str;
 6      str[0]=H;
 7      funstr(a);
 8      return 0;
 9 } 
10 void funstr(char str[12])
11 {
12     int t=0;
13     printf("%s\n",str);
14     while(str[t]!=\0)
15     {
16         printf("%c",str[t]);
17         t++;
18     }
19 }
正确的代码

更改并纠正了以上错误,进行了反思

四、

技术分享

 

五、

编程时需要有足够的细心程度,用脑且用心,字符串需要多定义一个长度 需要初始化地址及变量。

六、尊重正版、谨防假冒伪劣:(      哼!!!!

第二次作业补充

标签:细节   close   循环条件   指针   images   运行   std   bsp   style   

原文地址:http://www.cnblogs.com/1623025417qq/p/6652614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!