c - 如何将函数的指针返回给指针,C语言

标签 c pointers data-structures

以下是我编写的使用链表数据结构实现堆栈的代码。我了解数据结构的概念,但对使用指针还很陌生,因此在实现时遇到了麻烦。

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

    typedef struct NODE{
        int data;
        struct NODE *next;
    }NODE;

    NODE *top=NULL;
    NODE *newNode=NULL;

    NODE createNode();
    void push(NODE *newNode, int element);
    int pop();
    void display();

    int main()
    {
        int element,choice,p_item;

        do
        {
            printf("\nWhat operation would you like to perform on stack?\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
            printf("\nEnter your choice: ");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: printf("\nScan an element to push on stack: ");
                        scanf("%d",&element);
                        push(newNode, element);
                        break;

                case 2: p_item=pop();
                        printf("\nThe popped item is %d.\n",p_item);
                        break;

                case 3: display();
                        break;

                case 4: exit(0);
                        break;

                default: printf("\nWrong Choice!!Enter Again!\n");
            }
        }while(choice!=4);

        return 0;
    }

int pop()
{   
    if(top==NULL)
    {
        printf("\nStack UnderFlow!\n");
        return 0;
    }
    NODE *temp=top;
    int p_item;

        p_item=top->data;
        top=top->next;
        free(temp);

    return p_item; 
}

void display()
{
   if(top == NULL)
        printf("\nStack UnderFlow!\n");
   else
   {
        NODE* temp = top;
        while(temp->next != NULL)
        {
            printf("%d--->",temp->data);
            temp=temp->next;
        }  
        printf("%d--->NULL",temp->data);
   }
}

上面写的代码大部分是正确的,唯一遇到问题的地方是下面的部分:

    NODE createNode()
    {   NODE *node;
        node=(NODE*)malloc(sizeof(NODE));
        return node;  //line 54
    }

在上面的函数中,我相信我正在做的是;声明一个节点,为其分配内存并返回指针。但显然代码不正确,我的知识有限,无法理解,我做错了什么。

    void push(NODE *newNode, int element)
    {
        newNode=createNode();  //line 59
        // newNode=(NODE*)malloc(sizeof(NODE));
        newNode->data=element;
        if(top==NULL)
        {
            newNode->next=NULL;
            top=newNode;
            return;
        }
        newNode->next=top;
        top=newNode;
    }

在上面的代码中,如果我省略了

    newNode=createNode();

并取消注释以下行,

    newNode=(NODE*)malloc(sizeof(NODE));

然后,我可以毫不费力地完美地执行整个程序,但是为了实现其他数据结构概念,我想了解哪里出了问题。

错误如下:

stack_using_linked_list.c: In function ‘createNode’:
stack_using_linked_list.c:54:12: error: incompatible types when returning type ‘NODE * {aka struct NODE *}’ but ‘NODE {aka struct NODE}’ was expected
     return node;
            ^
stack_using_linked_list.c: In function ‘push’:
stack_using_linked_list.c:59:12: error: incompatible types when assigning to type ‘NODE * {aka struct NODE *}’ from type ‘NODE {aka struct NODE}’
     newNode=createNode();

感谢您花时间阅读。

最佳答案

In above function, what I believe I am doing is; declaring a node, allocating it memory and returning the pointer. But apparently the code is incorrect and my knowledge is limited to understand, what I am doing wrong.

我看到的错误是您将函数声明为返回 NODE ,但您的代码返回 NODE* (指向 NODE 的指针)。

您可以更改函数的声明以显示正确的返回类型。

NODE* createNode()
{
    NODE *node;
    node = (NODE*)malloc(sizeof(NODE));
    return node;
}

或者您可以将其缩短为这个。

NODE* createNode()
{
    return (NODE*)malloc(sizeof(NODE));
}

关于c - 如何将函数的指针返回给指针,C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50420424/

相关文章:

java - JNA 传递的值未更新

c - 如何从 C 中的字符串中获取没有空格的 int?

c - C中局部变量的地址赋给全局指针?

javascript - 遍历JavaScript中新引入的Map数据结构的时间复杂度是多少?

c - 字符串中间的空终止符

C编程: Check if the IP address is added on any given NIC

c++ - 这两个陈述有什么不同

c - 指针值在函数调用过程中发生变化

java - TreeMap : Sort values of a map with keys moving along with values

创建用 C 中的链表实现的通用堆栈