首页 新闻 会员 周边

C语言 结构体指针无法在Init函数中初始化

0
悬赏园豆:10 [已解决问题] 解决于 2015-10-15 12:02
定义一个队列结构体,想用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 <stdio.h>
#include <stdlib.h>

//定义数据结构

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;
}
那个十四号的主页 那个十四号 | 初学一级 | 园豆:195
提问于:2015-10-13 11:37
< >
分享
最佳答案
1
typedef struct _queue
{
QNode *qfront;
QNode *qrear;
}Queue,*LinkedQueue;

 

改为

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

 

就好了

你定义了一个双指针类型。

收获园豆:10
jiajia_ | 菜鸟二级 |园豆:234 | 2015-10-13 12:02

你好!可是我虽然定义了 typedef struct _queue *LinkedQueue这个指针,后面并没有用啊。用的还是Queue。

那个十四号 | 园豆:195 (初学一级) | 2015-10-13 12:11

@不打dota的ITer: 

编译器认为Queue已经是一个指针类型了,至于为什么会这样,我也不清楚。

我一般不这样写代码,不把握。

你把*去掉就可以了的。

jiajia_ | 园豆:234 (菜鸟二级) | 2015-10-13 13:15

@jiajia_: 谢谢你的回答,我已经弄明白了,函数这样写就行了

Status InitQueue(Queue **q)    //2级指针传参
{
    *q = (Queue*)malloc(sizeof(Queue));
    if(*q == NULL)
        return OVERFLOW;
    (*q)->front = (*q)->rear = (QNode*)malloc(sizeof(QNode));
    if((*q)->front == NULL)
        return OVERFLOW;
    (*q)->front->next = NULL;
        return OK;
}

即,需要传递指针的指针

那个十四号 | 园豆:195 (初学一级) | 2015-10-15 11:48

分给你了,谢谢回复

那个十四号 | 园豆:195 (初学一级) | 2015-10-15 12:02
其他回答(1)
0

感谢http://my.csdn.net/a1193561652的指点,已经解决问题。

struct定义一个指针p后,指针的值是无效的地址。欲在Init函数中用malloc分配内存将将地址赋值给p,那么就应该在Init函数中传递p的地址&p,也就是一个二级指针,然后在子函数中*p=malloc。如果直接传递p,显然传递的是一个野地址,是无效的。

粘贴代码如下:

Status InitQueue(Queue **q)    //2级指针传参
{
    *q = (Queue*)malloc(sizeof(Queue));
    if(*q == NULL)
        return OVERFLOW;
    (*q)->front = (*q)->rear = (QNode*)malloc(sizeof(QNode));
    if((*q)->front == NULL)
        return OVERFLOW;
    (*q)->front->next = NULL;
        return OK;
}

同理,加入在destroy函数中,如果不仅想将p->front和p->rear置空,而且要将p置空以免指向野地址,代码如下:

Status SetQueueNULL(Queue **q)
{
    if(q==NULL) return OK;
    ClearQueue(*q);
    free((*q)->front);
    (*q)->front=(*q)->rear=NULL;
    free(*q);
    *q=NULL;
    return OK;
}

 

那个十四号 | 园豆:195 (初学一级) | 2015-10-15 11:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册