| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 343 人关注过本帖
标题:scanf()函数深入研究,求解
只看楼主 加入收藏
t1melost
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2010-12-6
结帖率:70%
收藏
已结贴  问题点数:20 回复次数:1 
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
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:20 
正是因为你只给它分配了两个字节,才导致了空间不够用,而不是你所想的 装了还能装
字符串是以 '\0' 结束的,也就是说字符串 "aa" 其实是 "aa\0"。
两个字节装不下最后的 "\0", 也就是说由 p1->classify 所指向的字符串并没有在 "aa" 后结束。
而距 p1->classify 所指地址最近的 '\0' 就在 "bb" 后面,所以。。。

明白了吧

实际上当你读取 classify 的时候内存里是这样的:


classify
    \/
    'a' 'a'
'\0'
             /\
            name


读取 name 后就成了


classify
    \/
    'a' 'a'
'b' 'b' '\0'
             /\
            name


[ 本帖最后由 voidx 于 2011-4-25 21:28 编辑 ]
2011-04-25 21:22
快速回复:scanf()函数深入研究,求解
数据加载中...
 
   



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

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