august 发表于 2007-11-29 00:45:36

为何无法运行


#include<stdio.h>
#include<stdlib.h>
struct qnode
{
    char data;
    struct qnode *next;
   
   
};
typedef struct queue
{
    struct qnode *rear;
}linkqueue;
void initqueue(linkqueue *q)
{
    q=(struct queue *)malloc(sizeof(struct queue));
    q->rear=NULL;
}
void enter(linkqueue *q,char x)
{
    struct qnode *s,*p;
   
    s=(struct qnode *)malloc(sizeof(struct qnode));
    s->data=x;
    if(q->rear==NULL)
    {
      q->rear=s; s->next=s;
    }
    else
    {
      p=q->rear->next;
      q->rear->next=s;
      q->rear=s;s->next=p;
    }
}
void del(linkqueue *q)
{
    struct qnode *t;
    if(q->rear==NULL)
    {
      printf("队列为空\n");
      
    }
    else if(q->rear->next==q->rear)
    {
      t=q->rear;
      q->rear=NULL;
    }
    else
    {
      t=q->rear->next;
      q->rear->next=t->next;
    }
    free(t);
}
char gethead(linkqueue *q)
{
    if(q->rear==NULL)
      printf("队列为空\n");
    else
      return(q->rear->next->data);
    return 0;
}
int empty(linkqueue *q)
{
    if(q->rear==NULL) return 1;
    else return 0;
}
void dis(linkqueue *q)
{
    struct qnode *p=q->rear->next;
    printf("队列元素:");
    while(p!=q->rear)
    {
      printf("%c ",p->data);
      p=p->next;
    }
    printf("%c\n",p->data);
}
void main()
{
   
    linkqueue *qu;
   
    printf("初始化\n");
    initqueue(qu);
    printf("输入a,b,c\n");
    enter(qu,'a');
    enter(qu,'b');
    enter(qu,'c');
    dis(qu);
}

为什么在VC++里无法运行呢??

rednaxela 发表于 2007-11-29 11:53:53

不只VC,其它C编译器也应该无法让它运行吧。
从main开始看的话,一开始就有错。

linkqueue *qu;
声明了一个linkqueue指针。

initqueue(qu);
执行了这个调用后,qu应该还是一个任意的杂乱数据(VC的话,在调试模式下印象中是0xCDCDCDCD?现在用的这台机上没开发环境,无法测试)

原因……请LZ仔细想想指针、引用、按值传递、按引用传递的关系

august 发表于 2007-11-29 22:48:35

在TC上可以???

rednaxela 发表于 2007-11-30 11:17:06

你可以尝试一下在initqueue()的前后都加上一句打印qu的数值的语句来看我说的对不对:
printf("0x%X\\n", qu);

在initqueue()的里面,malloc()之后也加一句printf()来对比下:
printf("0x%X\\n", q);
页: [1]
查看完整版本: 为何无法运行