How to properly return a Pointer from a function to another function?

Viewed 62

I was writing a small program using pointers, This is my code:

    #include<iostream>

using namespace std;

struct number
{
int data;
number *next;
} test;

number * last_node(number *head)
{
    number *temp = new number;
    temp = head;

    while (temp->next != nullptr)
    {
        temp = temp->next;
    }

    return temp;

}

void insert_at_end (number *head)
{
    number *temp = new number;
    number *last = new number;
last = last_node(*head);

    cout<<"Enter the data";
    cin>>temp->data;
    temp->next = nullptr;
    last->next = temp;
}

void view_all (number *head)
{
    number *temp = new number;
    temp = head;

    while (temp->next != nullptr)
    {
        cout<<temp->data;
        temp = temp->next;
    }
}

int main()
{
    number *head = &test;
    int choice;
    char ans = 'y';

    do
    {



    cout<<"Enter your choice";
    cout<<"\n 1. Insert ";
    cout<<"\n 2. View ";
    cout<<"\n 3. Exit ";

    cin>>choice;

    switch (choice)
    {
        case 1 : insert_at_end(head);
        break;

        case 2 : view_all(head);
        break;

        case 3 : goto label;

        default : cout<<"You have entered wrong choice";
    }

    cout<<"Do you want the menu again ? (y/n)" ;
    cin>>ans;

    }

    while (ans = 'y');

    label:

        return 0;
}

On compilation, an error occurs on saying it can't convert "number" to "number*" while calling the function 'last_node(*head)'.

Can you please help me understand this properly?

1 Answers
Related