首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的结构体数组

C中的结构体数组
EN

Stack Overflow用户
提问于 2015-09-28 18:44:28
回答 3查看 123关注 0票数 0

我只是在刷我的指针概念,似乎随着时间的推移它真的搞砸了。

我试图在二叉树中实现BFS。

伪码:

代码语言:javascript
复制
1) tempNode = root node
2) while tempNode is not null
       print data at tempNode
       enqueue left and right child
       tempNode = dequeue

这是我的密码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>

#define MAX_Q_SIZE 100
/**
 * Using Queue to keep track of next nodes to be visited
 */

struct node
{
    int data;
    struct node *left;
    struct node *right;
};

struct node *createQueue(int *front, int *rear)
{
    struct node *queue = (struct node*)malloc(sizeof(struct node) * MAX_Q_SIZE);
    *front = *rear = 0;
    return queue;
}

void enQueue(struct node *queue, int *rear, struct node *newNode)
{
    queue[*rear].data = newNode->data;
    queue[*rear].left = newNode->left;
    queue[*rear].right = newNode->right;
    (*rear)++;
}

struct node *deQueue(struct node *queue, int *front)
{
    (*front)++;
    return &queue[*front - 1];
}

struct node *newNode(int data)
{
    struct node *node = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

void printBFS(struct node *root)
{
    int front,rear;
    struct node *queue = createQueue(&front, &rear);
    struct node *tempNode = root;

    while(tempNode != NULL)
    {
        printf("%d ",tempNode->data);

        if(tempNode->left != NULL)
            enQueue(queue,rear,tempNode->left);
        if(tempNode->right != NULL)
            enQueue(queue,rear,tempNode->right);
        tempNode = deQueue(queue,front);
    }
}

int main()
{
    struct node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);

    printBFS(root);
    return 0;
}

我收到以下四种警告:

代码语言:javascript
复制
BFS2.c: In function ‘printBFS’:
BFS2.c:60:13: warning: passing argument 2 of ‘enQueue’ makes pointer from integer without a cast [enabled by default]
             enQueue(queue,rear,tempNode->left);
             ^
BFS2.c:23:6: note: expected ‘int *’ but argument is of type ‘int’
 void enQueue(struct node *queue, int *rear, struct node *newNode)
      ^

BFS2.c:63:9: warning: passing argument 2 of ‘deQueue’ makes pointer from integer without a cast [enabled by default]
         tempNode = deQueue(queue,front);
         ^
BFS2.c:34:14: note: expected ‘int *’ but argument is of type ‘int’
 struct node *deQueue(struct node *queue, int *front)

有谁能帮我澄清我为何会收到警告的疑问?我在这里找不出问题。:(

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-28 18:52:29

当前的指针问题来自试图将int作为指针传递:

代码语言:javascript
复制
int front,rear;
    ...
        enQueue(queue,&rear,tempNode->left);
//         enQueue(queue,rear,tempNode->left);

紧随其后的deQueue也有同样的问题。然而,您有更大的问题要处理。一旦解决了指针问题,您将面临一个Segmentation fault

为了不让您挂起,您只需返回并重新工作您的队列实现,这是一个灾难。您将生成分段错误,因为frontdeQueue中的值不受检查,直到我达到100值,然后尝试写入超出MAX_Q_SIZE内存块结尾的位置。

如果您正在浏览指针,那么不要折磨指针来尝试将它们压缩到数组语法中。不需要传递struct node *queue,然后尝试查找能够工作的数组索引语法:

代码语言:javascript
复制
queue[*rear].right = newNode->right;

当一个简单的指针表达式可以完成时:

代码语言:javascript
复制
(queue + *rear)->right = newNode->right;

同样适用于:

代码语言:javascript
复制
return (queue + *front - 1);  // syntax only your queue logic need fixing

这将大大提高代码的可读性,并减少在传递指针时出错的可能性,但随后尝试使指针工作于以下内容:

代码语言:javascript
复制
return &queue[*front - 1];
票数 2
EN

Stack Overflow用户

发布于 2015-09-28 18:50:46

在传递int变量时,需要使用int* (指向int的指针)。传递int变量的地址和&操作符的地址。多个例子:

代码语言:javascript
复制
enQueue(queue, &rear, tempNode->left);

enQueue(queue, &rear, tempNode->right);

tempNode = deQueue(queue, &front);

正如David所指出的,该程序将崩溃(分段错误)。您可能需要重新考虑deQueue的设计和/或如果tempNode->lefttempNode->right都是NULL应该发生什么。

票数 4
EN

Stack Overflow用户

发布于 2015-09-28 18:51:30

而不是将frontrear传递给enQueue()deQueue(),而是传递&front&rear& ( address -of操作符)接受变量的地址。因为frontrear都是int的,所以这个操作符将创建正确的结果,即int *

作为旁白:malloc()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32829759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档