Function returning a linked list

Hi, mates.
I wrote the function and it runs properly, but i don't know how to make it return a List object correctly.
It should be like:
List list = Function(arguments);
but when i type it thatway it crushes.
Should my function be of a List& type ?
Thank you in advance!
Last edited on
Usually, when you want to pass around objects more than a few bytes long, you pass a pointer to them, instead.
So you mean i should have List* Function(arguments)
and then List* list = Function(arguments) ?
how to use list then ?
Last edited on
What do you mean "how to use it"? You use it like you would any other pointer.
I mean when "list" is a pointer, how can i use list's methods i defined i its class ?
like print() for example ?
You use it like you would any other pointer.


I.e.:
1
2
3
List list;
List* pointer = &list;
pointer->stuff();
I guess I'm too stupid but it doesn't work for me...
My code is like that :
1
2
3
4
5
6
7
8
List Function (arguments)
{
    ...
   return list;
}

List* list = &Function(arguments);
list->print()


Everything else works fine... even if i move the body of the function to the main() it works as i want it to work... but that problem with returning a list I cant fix :(
You can't do: List* list = &Function(arguments);

IIRC, this tries to find the data address of the function...make the function return a List* then just set the List* you are creating to the result of the function.
But if i try :
1
2
3
4
5
6
7
8
9
10
List Function (arguments)
{
    ...
   List* asdf = &list
   return asdf;
}

List* list = Function(arguments);
list->print()
}

it crushes again :(
Last edited on
Why does the signature say the function returns a List instead of a List *?

You're trying to return a pointer to a local object. Local objects are destructed when the function that created them returns, so that means you're returning an invalid pointer.
Read this on dynamic memory allocation:
http://www.cplusplus.com/doc/tutorial/dynamic.html
Last edited on
so we can :
return list;???
you can return a pointer and from that Pointer you can move further ....

like

if you return ptr ; <ptr is a Pointer >

than you can go like this ptr->next->next ..... and so on to yur desired place ..
You just need to return the head node of the list, since the list is made up of pointers, the head node would be pointing to the next node.
Ok, but how can i use a pointer to a list, which is destructed then ? :(
Then i should return the list itself ??
i wanted it to be something like that :
1
2
3
4
5
6
7
8
9
10
11
List* Function (List& list1, List& list2)
{
   List list = list1;
    ...
   List* asdf = &list
   return asdf;
}

List* list = Function(arguments);
list->print()
}

I understood that after returning the pointer asdf, list is destructed... Can i saomehow preserve destructing it while executing the whole program or is there any other appropriate solution ?
Last edited on
I didn't post this link to show the pretty colors:
http://www.cplusplus.com/doc/tutorial/dynamic.html
sorry and thank you, helios!
Topic archived. No new replies allowed.