标题:一个很简单的问题,但我解决不了
只看楼主
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
结帖率:0
已结贴  问题点数:2 回复次数:13 
一个很简单的问题,但我解决不了
下面程式的问题是当打入学校名字,但输出的是一个字元而不是我输入的学校名称字串

程序代码:
#include <stdio.h>
#include <stdlib.h>

struct rating{
    int rating_2011;
    int rating_2012;
};

struct school{
    char name[20];
    struct rating rating;
    int school_num;
};

int main ()
{
    char a[20];
    int b,c,d;
    printf("plz enter your school name:\n");
    scanf("%s",a);
    printf("plz enter your school rating in 2011:\n");
    scanf("%d",&b);
    printf("plz enter your school rating in 2012:\n");
    scanf("%d",&c);
    printf("plz enter your school number:\n");
    scanf("%d",&d);
    
    struct school no1={a,{b,c},d};
    struct school no2;
    
    no2=no1;
    
    printf("the name of your school is %s\n", no1.name);
    printf("the rating of %s in 2011 is %d\n",no1.name,no1.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no1.name,no1.rating.rating_2012);
    printf("the school number of %s is %d\n", no1.name,no1.school_num);
    
    printf("\n");
    
    printf("the name of your school is %s\n", no2.name);
    printf("the rating of %s in 2011 is %d\n",no2.name,no2.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no2.name,no2.rating.rating_2012);
    printf("the school number of %s is %d\n", no2.name,no2.school_num);
    
    printf("\n");
    
    system("Pause");
    return 0;
}

搜索更多相关主题的帖子: 名字 school rating 
2011-06-04 18:44
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:1 
struct school no1={a,{b,c},d};
这句初始化有问题。我不知道你用的是什么编译器,这句居然能通过?允许用非常数数组初始化数组?

重剑无锋,大巧不工
2011-06-04 18:59
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
得分:1 
汗,难道是GCC?

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2011-06-04 19:49
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
得分:0 
我用的是xcode编译器
2011-06-04 19:49
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
得分:0 
不对。VC也支持变量作为初时化列表的

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2011-06-04 19:50
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
得分:1 
    struct school no1={a,{b,c},d};
你不如直接声明no1,输入no1.name……
2011-06-04 19:56
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:0 
我用的是GCC,不支持这样初始化。但你的问题恐怕还是在这里,你那句我理解是将a的地址赋值给了name数组的第一个元素了。虽然a的地址为4字节,但赋值过程应该截段成一个字节了。告诉我的你执行结果,验证一下。

重剑无锋,大巧不工
2011-06-04 20:01
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
得分:0 
对不起,上面的程式码打错了,我打的是下面这个,a有引号
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct rating{
    int rating_2011;
    int rating_2012;
};

struct school{
    char name[20];
    struct rating rating;
    int school_num;
};

int main ()
{
    char a[20];
    int b,c,d;
    printf("plz enter your school name:\n");
    scanf("%s",a);
    printf("plz enter your school rating in 2011:\n");
    scanf("%d",&b);
    printf("plz enter your school rating in 2012:\n");
    scanf("%d",&c);
    printf("plz enter your school number:\n");
    scanf("%d",&d);
    
    struct school no1={"a",{b,c},d};
    struct school no2;
    
    no2=no1;
    
    printf("the name of your school is %s\n", no1.name);
    printf("the rating of %s in 2011 is %d\n",no1.name,no1.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no1.name,no1.rating.rating_2012);
    printf("the school number of %s is %d\n", no1.name,no1.school_num);
    
    printf("\n");
    
    printf("the name of your school is %s\n", no2.name);
    printf("the rating of %s in 2011 is %d\n",no2.name,no2.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no2.name,no2.rating.rating_2012);
    printf("the school number of %s is %d\n", no2.name,no2.school_num);
    
    printf("\n");
    
    system("Pause");
    return 0;
}


执行结果
程序代码:
plz enter your school name:
Tom
plz enter your school rating in 2011:
3
plz enter your school rating in 2012:
2
plz enter your school number:
456
the name of your school is a
the rating of a in 2011 is 3
the rating of a in 2012 is 2
the school number of a is 456

the name of your school is a
the rating of a in 2011 is 3
the rating of a in 2012 is 2
the school number of a is 456
2011-06-04 21:02
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
得分:0 
整数输出是正确,但字串输出是错误,它直接变为a
2011-06-04 21:05
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:0 
没问题啊,你本来就是用一个常字符串初始化的name数组嘛。
建议你在下面加一句strcpy(no1.name, a);,包含头文件string.h。

重剑无锋,大巧不工
2011-06-04 21:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-341899-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.310797 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved