如何从函数内部使指针(指向结构)为空 [英] How to NULL a pointer (to a struct) from within a function

查看:96
本文介绍了如何从函数内部使指针(指向结构)为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个班轮问题:
有人可以解释一下thisNode=NULL为什么不能在函数中起作用,以及如何以其他方式实现相同的结果.

One Liner Question:
Can someone please explain why thisNode=NULL doesn't work in a function and how I can achieve the same result in some other way.

简介:
在开始研究数据结构时,我最近开始使用指针来构造结构.

Introduction:
I have started using pointers to structs a lot recently as I have started studying Data Structures.

历史记录:
我已经对堆栈,队列,二进制搜索树和表达式树进行了编码,并将很快开始对avl树进行编码.作为一名学生,我仍然不了解许多概念,希望能对您有所帮助.

History:
I have coded stacks, queues, binary search trees and expression trees and will start coding an avl tree soon. Being a student I still am unaware of many concepts and would appreciate any help.

方法:
我总是找到其他方法来达到所需的结果(清空头节点),例如在main函数中将其清空,或者最终根本不将其清空,并将其保留为带有空数据的头/根节点,然后添加其他(有用的节点). (仅将其用作结构的头或指针)

Methodology:
I always find other ways to achieve my required result (nulling the head node), like nulling it in the main function, or end up not nulling it at all and keeping it as a head/root node with null data and then adding other (useful) nodes to it. (Only use it as a head or pointer to the structure)

困惑:
我不明白为什么从函数内将结构的指针为空不起作用,但是诸如更改结构数据成员的值之类的其他事情却没有问题.

Confusion:
I do not understand why nulling a pointer to struct from within a function doesn't work but other things such as changing the structs data member's values work with no problems.

例如:

thisNode->nextNode->data = thisNode->data; *//this does works*
thisNode = NULL; *//this doesn't work*


图片:

问题:
有人可以解释一下thisNode=NULL为什么不能在函数中起作用,以及我如何以其他方式实现相同的结果.

Question:
Can someone please explain why thisNode=NULL doesn't work in a function and how I can achieve the same result in some other way.

问题区域:

void stackPop(stackNode *headNode){
    if (headNode!=NULL){
        if (headNode->nextNode!=NULL){
            stackNode *tempNode = getLatestStackNode(headNode);
            tempNode->prevNode->nextNode=NULL;
            tempNode->prevNode==NULL;
            tempNode->data=='.';
            tempNode=NULL;
            free(tempNode);
        }else if (headNode->nextNode==NULL){
            headNode->data='\0';
            headNode = NULL;
            free(headNode);
        }
    }else{
        printf("\nstack currently has no nodes\n");
    }
}

整个堆栈代码:

#include <stdio.h>
#include <stdlib.h>


typedef struct stackNode{
    char data;
    struct stackNode *nextNode;
    struct stackNode *prevNode;
}stackNode;

stackNode* createStackNode(char value){
    stackNode *newStackNode = (stackNode*) malloc(sizeof(stackNode));
    newStackNode->nextNode=NULL;
    newStackNode->prevNode=NULL;
    newStackNode->data=value;
    return newStackNode;
}

stackNode* getLatestStackNode(stackNode *headNode){
    stackNode *tempNode = headNode;
    while (tempNode->nextNode!=NULL){
        tempNode=tempNode->nextNode;
    }
    return tempNode;
}

stackNode* stackPush(stackNode *headNode, char value){
    if (headNode==NULL){
        return createStackNode(value);
    } else {
        stackNode *tempNode = getLatestStackNode(headNode);
        tempNode->nextNode = createStackNode(value);
        tempNode->nextNode->prevNode=tempNode;
        return tempNode->nextNode;
    }
}

void stackPop(stackNode *headNode){
    if (headNode!=NULL){
        if (headNode->nextNode!=NULL){
            stackNode *tempNode = getLatestStackNode(headNode);
            tempNode->prevNode->nextNode=NULL;
            tempNode->prevNode==NULL;
            tempNode->data=='.';
            tempNode=NULL;
            free(tempNode);
        }else if (headNode->nextNode==NULL){
            headNode->data='\0';
            headNode = NULL;
            free(headNode);
        }
    }else{
        printf("\nstack currently has no nodes\n");
    }
}

char stackPeek(stackNode *headNode){
    stackNode *tempNode = getLatestStackNode(headNode);
    return tempNode->data;
}

void stackPeekPrint(stackNode *headNode){
    stackNode *tempNode = getLatestStackNode(headNode);
    printf("\n%c\n",tempNode->data);
}

void isNULL(stackNode *headNode){

    if (headNode==NULL){
        printf("\nIt is NULL\n");
    } else if (headNode!=NULL) {
        printf("\nIt isn't NULL\n");
    }
}

int main(){

    stackNode *x = stackPush(NULL,'x');
    stackPush(x,'u');
    stackPush(x,'w');

    stackPop(x);
    stackPop(x);
    stackPop(x);

    stackPeekPrint(x);
    isNULL(x);

}

推荐答案

在C中,所有参数均按值传递.在stackPop中,headNode作为参数传递,因此对它的任何更改都不会反映在调用函数中.

In C, all parameters are passed by value. In stackPop, headNode is passed as a parameter, so any changes to it are not reflected in the calling function.

您需要从main传入x的地址,以便可以对其进行修改.

You need to pass in the address of x from main so that it can be modified.

因此将stackPop更改为此:

void stackPop(stackNode **headNode){     //  takes a pointer-topointer
    if (*headNode!=NULL){
        if ((*headNode)->nextNode!=NULL){
            stackNode *tempNode = getLatestStackNode(*headNode);
            tempNode->prevNode->nextNode=NULL;
            // no need to clear fields before freeing
            free(tempNode);
        }else if ((*headNode)->nextNode==NULL){
            // no need to clear fields before freeing
            free(*headNode);     // free first, then set to NULL
            *headNode = NULL;
        }
    }else{
        printf("\nstack currently has no nodes\n");
    }
}

并这样称呼它:

stackPop(&x);

这篇关于如何从函数内部使指针(指向结构)为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆