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

大家看看这是不是c的bug?该怎么处理

2012-03-11 
大家看看这是不是c的bug?#includestdio.h#include malloc.h#includestring.htypedefstructDuLNode{c

大家看看这是不是c的bug?
#include   <stdio.h>
#include <malloc.h>
#include   <string.h>
typedef   struct   DuLNode
{
        char       data[21];
        struct   DuLNode       *up;
        struct   DuLNode       *next;
}DuLNode,   *DuLinkList;
void   function1(DuLinkList   fptr,DuLinkList   xptr)
{
fptr=(DuLinkList)malloc(sizeof(DuLNode));
xptr=fptr;
strcpy(xptr-> data, "123 ");
fptr-> up=fptr-> next=NULL;
printf( "xptr   is   %d\n ",xptr);
printf( "xptr-> data   is   %s\n ",xptr-> data);
printf( "xptr-> up   is   %d\n ",xptr-> up);
printf( "xptr-> next   is   %d\n ",xptr-> next);
return;
}
void   function2(DuLinkList   fptr,DuLinkList   xptr)
{

strcpy(xptr-> data, "123 ");
printf( "xptr   is   %d\n ",xptr);
printf( "xptr-> data   is   %s\n ",xptr-> data);
printf( "xptr-> up   is   %d\n ",xptr-> up);
printf( "xptr-> next   is   %d\n ",xptr-> next);
}
void   main()
{
DuLinkList   FPTR,XPTR;
function1(FPTR,XPTR);
function2(FPTR,XPTR);
}
为什么出现“遇到问题需要关闭”?
明明没有错啊?

[解决办法]
在function2中 实参只是个指针而已 它没有指向任何实际内存
[解决办法]
function2(FPTR,XPTR);中的XPTR 指向0x00000000 或者是0xcccccccc 那么XPTR-> xxxx都是非法的.

function1中是用malloc在堆里申请内存了, 所以可以访问结构体成员
[解决办法]
function1中要对指针进行操作,应该传递的是指针地址,及二级指针,
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct DuLNode
{
char data[21];
struct DuLNode *up;
struct DuLNode *next;
}DuLNode, *DuLinkList;
void function1(DuLinkList *fptr,DuLinkList *xptr)
{
*fptr=(DuLinkList)malloc(sizeof(DuLNode));
*xptr=*fptr;
strcpy((*xptr)-> data, "123 ");
(*fptr)-> up=(*fptr)-> next=NULL;
printf( "xptr is %d\n ",*xptr);
printf( "xptr-> data is %s\n ",(*xptr)-> data);
printf( "xptr-> up is %d\n ",(*xptr)-> up);
printf( "xptr-> next is %d\n ",(*xptr)-> next);
return;
}
void function2(DuLinkList fptr,DuLinkList xptr)
{

strcpy(xptr-> data, "123 ");
printf( "xptr is %d\n ",xptr);
printf( "xptr-> data is %s\n ",xptr-> data);
printf( "xptr-> up is %d\n ",xptr-> up);
printf( "xptr-> next is %d\n ",xptr-> next);
}
void main()
{
DuLinkList FPTR,XPTR;
function1(&FPTR,&XPTR);
function2(FPTR,XPTR);
}

热点排行