结构体指针初始化问题

定义一个队列结构体,想用init方法初始化一个该结构体的指针,testQueue1和testQueue2两种方法都有问题。Queue内部的front和rear指针无法初始化。
但如果是在主程序或者testQueue函数中用malloc初始化,却是可以的。
main程序如下:
#include "Queue.h"
void testQueue1();
void testQueue2();
void testQueue3();
int main()
{
printf("\n");
testQueue1();
testQueue2();
testQueue3();
return 0;
}
void testQueue1()
{
Queue q;
InitQueue(q);
printf("\n");
}
void testQueue2()
{
Queue *q=InitQueue_H();
}
void testQueue3()
{
Queue *q;
q=malloc(sizeof(Queue));
q->qfront=malloc(sizeof(QNode));
}
头文件:
#include
#include
//定义数据结构
typedef int DATA;
typedef struct _qnode;
typedef struct _qnode
{
DATA data;
struct _qnode *next;
}QNode;
//本定义中,qfront和qrear作为头尾指针,并不存储数据
typedef struct _queue
{
QNode *qfront;
QNode *qrear;
}Queue,*LinkedQueue;
//初始化一个空队列,返回值1表示初始化成功,0表示初始化失败
int InitQueue(Queue *q);
//初始化,返货Queue指针
Queue *InitQueue_H();
//判断是否为空
int IsQEmpty(Queue *q);
程序实现:
#include "Queue.h"
int InitQueue(Queue *q)
{
q=(Queue
)malloc(sizeof(Queue));
if(q==NULL)return 0;
q->qfront=q->qrear=(QNode*)malloc(sizeof(QNode));
if(q->qfront==NULL)
return 0;
q->qfront->next=NULL;
return 1;
}
Queue InitQueue_H()
{
Queue *q=(Queue
)malloc(sizeof(Queue));
if(q==NULL)return NULL;
q->qfront=q->qrear=(QNode*)malloc(sizeof(QNode));
if(q->qfront==NULL)
return NULL;
q->qfront->next=NULL;
return q;
}
int IsQEmpty(Queue *q)
{
if(q==NULL||q->qfront==NULL||q->qrear==NULL) return;
return q->qfront==q->qrear;
}

main.cpp

 #include "Queue.h"
void testQueue1();
void testQueue2();
void testQueue3();
int main()
{
    printf("\n");
    testQueue1();
    testQueue2();
    testQueue3();
    return 0;
}

void testQueue1()
{
    Queue *q = NULL;
    InitQueue(&q);  //要在函数里改变q,就要传q的地址过去,也就是2级指针
    printf("front = %d, rear = %d\n",q->qfront,q->qrear);
    printf("\n");

}

void testQueue2()
{
    Queue *q = InitQueue_H();
    //输出q的front和rear的值发现他们相等,说明初始化成功,没有问题。
    printf("front = %d, rear = %d\n",q->qfront,q->qrear);
}

void testQueue3()
{
    Queue *q;
    q = (Queue*)malloc(sizeof(Queue));
    q->qfront = (QNode*)malloc(sizeof(QNode));  
}

Queue.h

 typedef int DATA;
typedef struct _qnode;
typedef struct _qnode
{
    DATA data;
    struct _qnode *next;
}QNode;

typedef struct _queue
{
    QNode *qfront;
    QNode *qrear;
}Queue,*LinkedQueue;

int InitQueue(Queue **q);   //用2级指针传递参数
Queue  *InitQueue_H();
int IsQEmpty(Queue *q);

Queue.cpp

int InitQueue(Queue **q)    //2级指针传参
{
    *q = (Queue*)malloc(sizeof(Queue));

    if(*q == NULL)
        return 0;
    (*q)->qfront = (*q)->qrear = (QNode*)malloc(sizeof(QNode));
    if((*q)->qfront == NULL)
        return 0;
    (*q)->qfront->next = NULL;
        return 1;
}
Queue *InitQueue_H()
{
    Queue *q=(Queue*)malloc(sizeof(Queue));
    if(q==NULL)
        return NULL;
    q->qfront = q->qrear = (QNode*)malloc(sizeof(QNode));
    if(q->qfront==NULL)
        return NULL;
    q->qfront->next=NULL;
        return q;
}
int IsQEmpty(Queue *q)
{
    if(q==NULL || q->qfront==NULL || q->qrear==NULL) 
        return 0;  //没写返回值
    return q->qfront==q->qrear;
}

把InitQueue()改了一下,还是认为testQueue2()没问题,具体改动我注释上了。

程序复制的各种问题自己脑补的改了下。
testQueue2();没发现问题。
testQueue1();要在InitQueue()里改变指针,所以参数要用2级指针。

您可以看我在博客园提问这个,代码清晰一些。csdn不知道显示为什么会这样

[code=c/c++]
void testQueue()
{
Queue oo;
Queue h;
Queue qqq1;
printf("\n");
}

[/code]
gcc单步调试,oo输出
$1 = {
qfront = 0xfffffffe,
qrear = 0x757088f6
}