首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

变量设置不同~结果不同~指针有关问题~

2013-12-21 
变量设置不同~~结果不同~~指针问题~~源程序里对member的设置是普通结构变量,本人将Family member改成Famil

变量设置不同~~结果不同~~指针问题~~
源程序里对member的设置是普通结构变量,本人将Family member改成Family *member,对应的将&member改成了member,更改之后能正常封装,但是运行之后进行几次输入就强制关闭程序了。想请教大侠这是为什么。源程序在下面http://my.oschina.net/u/1408882/blog/185409
更改之后的程序如下

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#define NAME_MAX 20
struct
{
    char *filename;
    FILE *pfile;
}global={"E:\\myfile.bin",NULL};
struct Date
{
    int day;
    int month;
    int year;
};
typedef struct family
{
    struct Date dob;
    char name[NAME_MAX];
    char pa_name[NAME_MAX];
    char ma_name[NAME_MAX];
}Family;
bool get_person(Family *pfamily);
void getname(char *name);
void show_person_data(void);
void get_parent_dob(Family *pfamily);
int main(void)
{
    Family *member;
    if(!(global.pfile=fopen(global.filename,"wb")))
    {
        printf("\nUnable to open %s for writing.\n",global.filename);
        exit(1);
    }
    while(get_person(member))
        fwrite(member,sizeof(member),1,global.pfile);
    fclose(global.pfile);
    show_person_data();
    if(remove(global.filename))
        printf("\nUnable to delete %s.\n",global.filename);
    else
        printf("\nDeleted %s OK.\n",global.filename);
    return 0;
}
bool get_person(Family *temp)
{
    static char more='\0';
    printf("\nDo you want to enter details of a%s person(Y or N)?",
           more!='\0'?"nother":"");
    scanf(" %c",&more);
    if(tolower(more)=='n')
        return false;
    printf("\nEnter the name of the person:");
    getname(temp->name);
    printf("\nEnter %s's date of birth (day month year);",temp->name);
    scanf("%d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year);
    printf("\nWho is %s's father?",temp->name);
    getname(temp->pa_name);
    printf("\nWho is %s's mother?",temp->name);
    getname(temp->ma_name);
    return true;
}
void getname(char *name)
{
    fflush(stdin);
    fgets(name,NAME_MAX,stdin);
    int len=strlen(name);
    if(name[len-1]=='\n')
        name[len-1]='\0';
}
void show_person_data(void)
{
    Family member;
    fpos_t current=0;
    if(!(global.pfile=fopen(global.filename,"rb")))
    {
        printf("\nUnable to open %s for reading.\n",global.filename);
        exit(1);
    }
    while(fread(&member,sizeof member,1,global.pfile))
    {
        fgetpos(global.pfile,&current);
        printf("\n\n%s's father is %s,and mother is %s",
               member.name,member.pa_name,member.ma_name);
        get_parent_dob(&member);
        fsetpos(global.pfile,&current);
    }
    fclose(global.pfile);
}
void get_parent_dob(Family *pmember)
{
    Family relative;
    int num_found=0;
    rewind(global.pfile);
    while(fread(&relative,sizeof(Family),1,global.pfile))
    {
        if(strcmp(pmember->pa_name,relative.name)==0)
        {
            printf("\nPa was born on %d/%d/%d",
                   relative.dob.day,relative.dob.month,relative.dob.year);
            if(++num_found==2)
                return;


        }
        else
            if(strcmp(pmember->ma_name,relative.name)==0)
        {
            printf("\nMa was born on %d/%d/%d.",
                   relative.dob.day,relative.dob.month,relative.dob.year);
            if(++num_found==2)
                return;
        }
    }
}


[解决办法]
有时候也不用malloc或者new,而是将指针指向一块已知的有效内存空间
[解决办法]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

热点排行