如何将指针从函数返回到指针C语言

时间:2018-05-18 23:31:46

标签: 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();

谢谢你,花时间阅读。

1 个答案:

答案 0 :(得分:4)

  

在上述功能中,我相信我所做的是;声明一个节点,   分配内存并返回指针。但显然是   代码不正确,我的知识仅限于理解,我是什么   做错了。

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

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

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

或者你可以缩短它。

NODE* createNode()
{
    return (NODE*)malloc(sizeof(NODE));
}
相关问题