| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 343 人关注过本帖
标题:scanf()函数深入研究,求解
取消只看楼主 加入收藏
t1melost
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2010-12-6
结帖率:70%
收藏
已结贴  问题点数:20 回复次数:0 
scanf()函数深入研究,求解
#include <stdio.h>
#include <math.h>

struct infor                                                            /////建立一个结构体
{
    long num;
    char classify[2];
    char name[20];
    char author[30];
    float price;
    struct infor *next;
};

void main()
{
    float temp=sqrt(0);

    int n=0;
    char ch;
    struct infor *head;
    struct infor *p1, *p2;                                                       /////建立链表,没有错,已验证。

    p1=p2=(struct infor*)malloc(sizeof(struct infor));

    printf("\nPlease input num     :");
    scanf("%ld",&p1->num);
    ch=getchar();                                                                  ////吃回车,你懂的。  
    printf("\nPlease input classify:");
    scanf("%s",p1->classify);                                                      ////问题来了,如果这里我输入aa ,            
    ch=getchar();                                                                  
    printf("\nPlease input books'name :");                                                   
    scanf("%s",p1->name);                                                               这里再输入bb,你们猜会怎么样,系统显示,P1->classify='aabb';
    ch=getchar();                                                                        我就纳闷了,为什么我明明定义classify是2个字节的数组,它却能
    printf("\nPlease input author's name:");                                                无缘无故地多装2个字节?
    scanf("%s",p1->author);
    ch=getchar();
    printf("\nPlease input price of the book:");
    scanf("%f",&p1->price);
    ch=getchar();
    printf("--------------------------");
    head=0;

    while(p1->num!=0)
    {
        n++;
        if(n==1)
            head=p1;
        else
            p2->next=p1;

        p2=p1;
        p1=(struct infor *)malloc(sizeof(struct infor));
        printf("--------------------------");
        printf("\nPlease input num     :");
        scanf("%ld",&p1->num);
        ch=getchar();
        printf("\nPlease input classify:");
        scanf("%s",p1->classify);
        ch=getchar();
        printf("\nPlease input books'name :");
        scanf("%s",p1->name);
        ch=getchar();
        printf("\nPlease input author's name:");
        scanf("%s",p1->author);
        ch=getchar();
        printf("\nPlease input price of the book:");
        scanf("%f",&p1->price);
        ch=getchar();
    }
    p2->next=NULL;
   
}

搜索更多相关主题的帖子: 结构体 
2011-04-25 21:13
快速回复:scanf()函数深入研究,求解
数据加载中...
 
   



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

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