|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
温故而知新,C基础知识还是不透彻,虽然已经开始学习汇编,但是调试结果还是看不懂
下面是错误代码,图片也是错误图片
问题自己成功解决,知道因为%s与%c引起的,但是调试的结果我却看不懂。
所以请求大神稍微讲解一下,错误代码如下
源代码如下
- #include <stdio.h>
- struct student
- {
- int num;
- char name[10];
- char sex;
- char job;
- union
- {
- int Class;
- char position[10];
- }category;
- }person[2];
- int main(void)
- {
- int i;
- for(i=0;i<2;i++)
- {
- printf("Please input the num: ");
- scanf("%d",&person[i].num);
- printf("Please input the name: ");
- scanf("%s",&person[i].name);
- printf("Please input the sex<M/F>: ");
- scanf("%s",&person[i].sex);
- printf("Please input the job<s/t>: ");
- scanf("%s",&person[i].job);
- if( 's' == person[i].job )
- {
- printf("Please input the class: ");
- scanf("%d",&person[i].category.Class);
- printf("\n\n");
- }
- else if( 't' == person[i].job )
- {
- printf("Please input the position: ");
- scanf("%s",&person[i].category.position);
- printf("\n\n");
- }
- else
- {
- printf("Please input s/t\n");
- }
- }
-
- printf("No.\t name \t sex \t job \t class/position\n");
- printf("%d %s %s %s %d",person[0].num,person[0].name,person[0].sex,person[0].job,person[0].category.Class); //输出失败 %s 改成 %c正常了就 因为输出的是一个字符
- printf("%d %s %s %s %s",person[1].num,person[1].name,person[1].sex,person[1].job,person[1].category.position); //输出失败
- return 0;
- }
复制代码
输出person数据的时候就卡壳了,本来想用循环输出,但是最后定义的Class,position输出类型不同(%d,%s),省事就这样了。
没有用调试之前,都是用printf("1")来测试,一段一段测代码有没有正常运行或者出错,一个一个得值来测试,效率低而且错误率高,最后发现是%s的问题。
下面是调试图片汇编代码看不懂,比如代表输出的%s或者%c或者输的汇编代码在那个段?总是下一步的时候找不到文件(Fiind files) 后期汇编学习进度跟上就懂了.....先求讲解
哥哥,这个还需要??刚好有人回复,我也尝试回答以下。
scanf(... &a); 需要透过地址才能赋值
char sex, job; //只有一个格,用 %s 需要两个格,越界了。
char name[10]; //数组名就是首地址,但跨度问题要注意。用 &name 其实是未定义行为,但是多数的编译器都认了。应该是 &name[0]
|
-
-
|