如何使函数在c ++中返回结构指针

时间:2013-01-13 04:07:13

标签: c++

我想让函数返回特定节点的地址。但编译器是 没有检测到我创建的节点数据类型结构。

struct node
{
int data;

node *link;
};

node *header,*current;
node traverse(int pos);


node *Linkedlist::traverse(int pos)
{
    int location = 0;  
    current->link = header->link;
    node *address = new node;
    address->data = NULL;
    address->link = NULL;


    while(current->link != NULL)
    {

        if(location == pos)
        {
            cout <<current->link->data <<" "<< endl; 
            address->link=current->link;
        }
        location ++;
        current->link = current->link->link;

    }


    return  address->link;
}

3 个答案:

答案 0 :(得分:5)

更改

return  *address;

return  address;

答案 1 :(得分:1)

由于地址是节点的指针变量,因此您只需返回变量。

指针变量之前的*是显式引用,这意味着获取指针变量address指向的值。这与运算符&完成的操作相反,获取变量的地址。

return  address;

因此,您应该返回变量,而不是返回变量指向的值。

注意,traverse的调用者需要小心,通过调用delete来显式释放内存,否则会导致内存泄漏。这是因为潜在的设计问题,您在本地范围内分配了一个堆对象并返回了该地址。

node * foo = Linkedlist::traverse(n);
...............
delete foo;

您可以简单地在堆中创建对象或将其添加为类成员,在前一种情况下,您可以轻松地将对象的所有权从一个范围转移到另一个范围,其中在第二种情况下,生命周期该对象的对象将由对象Linkedlist

控制

答案 2 :(得分:0)

我试过这个&#34;一个函数返回指针指针&#34;。 请忽略main()中的其他内容。

函数返回pointeris takedata();

如果您发现任何错误,或者您可以帮助获得更好的代码,那么欢迎您。

# include<iostream> #
using namespace std;
##include<conio.h>##
###include<string.h>###
struct dll
{
int data;
dll *next,*prev;
}*start,*last,*trav;

class doublell
{
 private:
    //dll *start,*last,*trav;
    int info;
public:
    doublell()
    {
        start=NULL;
        last=NULL;
        trav=NULL;
    }

    void create();
    dll *takedata();

    void display(); 
};

//  code for data to take from user
dll *doublell :: takedata( )
{       
int info;
    cout<<"\n enter Data:\n";
    cin>>info;
    dll *newnode1;
    newnode1= new dll;
    newnode1->data=info;
    newnode1->next=NULL;
    newnode1->prev=NULL;
    return (newnode1);

}


//  code to create double link list
 void doublell:: create()
{
    /*string e;
    cout<<"\n press any time end to terminate\n";*/
    char ch;
   while(1)
  {
    cout<<"\n do you want to terminate, press Y/y to terminate\n\n\n";
    ch=getch();
    if((ch=='y')||(ch=='Y'))
        {
            display();
            cout<<"\n\n";
            break;
        }
    else
        {
            dll *newnode;
            newnode= new dll;
            newnode=takedata( );

            /*cout<<"\n enter Data:\n";
            cin>>info;
            newnode->data=info;
            newnode->next=NULL;
            newnode->prev=NULL;*/

            if (start==NULL )
            {
                /*newnode->prev=start;
                newnode->next=last;*/
                start=newnode;
                last=newnode;
            }

            else
            {   
                last->next=newnode;
                newnode->prev=last;
            //newnode->next=NULL;
                last=newnode;
            }
        }
}
}



 /* cout<<"\n enter Data:\n";
    cin>>info;
    dll *newnode;
    newnode->data=info;
    newnode->next=NULL;
    newnode->prev=NULL;

   do{

        takedata();
        if (start==NULL && last==NULL)
        {
            start=last=newnode;
        }
        else
        {   
            newnode->pre=last;
            last=newnode;
        }
        cin>>e;
    }while(strcmp(e,"end")!=0)
}*/

//  code to display
void doublell:: display()
{   
    if (start==NULL)
    {
    cout<<"\n double link list is empty./n";
    return;
}
    else
    {
        dll *trav=start;
        dll *arrow= trav;           // arrow pointer is used for coreect 
 usage of --> @ last

    cout<<"\n start is:-";
    cout<<"\n"<<start->data;
    cout<<trav->data;
    getch();
    cout<<"\n\n NULL <-- ";
    while(trav!=NULL)
    {
            /*if(arrow==NULL)
            {
                cout<<trav->data<<"<--";
            }
            else*/

                cout<<trav->data<<" <--> ";
                trav=trav->next;
                arrow=trav;
                if((arrow->next)->next==NULL)
                {   
                    arrow=trav->next;
                    cout<<trav->data<<" <--> ";
                    cout<<arrow->data<<" --> ";
                    trav=(trav->next)->next;
                }

    }
    cout<<" NULL ";
}

 }

main()
{
char ch;
int c;
class doublell ok;

do{

    cout<<"\n 1> create list"<<endl;
    cout<<"\n 4> display list"<<endl;
    cout<<"\n Choose your choice: ";
    cin>>c;
    switch(c)
    {

        case 1:  
                ok.create();
                ok.display();
                break;              
        case 4: exit;
                break;
    }
}while(c!=4);

getch();


}