0

I see functions of the type:

data_type *function_name(arguments...)

and:

data_type function_name(arguments...)

I'm trying to learn when which one should be used. Why is this done in the example below? (This is from a textbook for a course.) When should I write functions like these?

struct Tower
{
    string name;
    Tower *link;
};

Tower *createTower(string name, Tower *link);

Further can someone clarify this?

Tower* createTower(string name, Tower *link)
{
    Tower* tp = new Tower;
    tp->name = name;
    tp->link = link;
    return tp;
}

Tower* createBeconsOfGondor(){
    Tower *tp = createTower("Rohan", NULL); // Points to nothing
    tp = createTower("Halifiren", tp); // Still returns pointer?
}

In the second line of the createBeconsOfGondor() function, how is the return of that function still a pointer?

4 Answers4

1

It means that the function returns a pointer, rather than an object.

For example

Tower createTower(string name, Tower *link);

returns a Tower object while

Tower *createTower(string name, Tower *link);

returns a pointer to a tower object.

sabreitweiser
  • 643
  • 3
  • 13
0

The function Tower *createTower(string name, Tower *link); returns a pointer to a Tower object. The asterisk before the function name is just specifying that the return type is a pointer.

You can also write the function definition this way which is equivalent but (in my opinion) more clear:

Tower* createTower(string name, Tower* link);
Eric Appelt
  • 2,843
  • 15
  • 20
  • Thanks, this makes it more clear. I now understand that whenever I want to return a pointer to a struct, this makes sense. Also, is this a function pointer? – greeneyedguy May 29 '15 at 13:21
  • A function pointer points to the memory address of an actual function, rather than some data type like an `int` or `struct`. For example, you could define `Tower* (*func_ptr) (string, Tower*)`. Then `func_ptr` can be set to the address of any function that takes a `string` and `Tower*` as arguments, and returns a `Tower*`. So you can set `func_ptr = &createTower` Then you can call `func_ptr(name, link)` and the same code as `createTower` will be executed. You can use function pointers to swap out function code as needed based on changing conditions. – Eric Appelt May 29 '15 at 13:42
  • Edited question, extended to ask a follow up question. – greeneyedguy May 29 '15 at 17:15
0

which one should be used.

Note that the first form:

Tower  createTower(...);

might be used as assignment (thus extending the instance's life), as in

...
Tower T1 = createTower(...);  
...

or, you might use as an anonymous temporary

 std::cout << createTower(...) << ...

If Tower is BIG, you might have motivation to keep the expensive instance around for more than just the print duration ... so maybe use the former and do

std::cout << T1 << ...

Else, if you are worried about stack space, and not so worried about remembering to not-leak-memory, then you have motivation to dynamically allocate the instance in the heap (this also supports a long-life-span)

 Tower* T1 = createTower(...);  // BIG tower in heap

 std::cout << *T1 << ....

 // .... and some time later, you should remember to 

 delete T1;

Others would recommend you use a smart pointer (and learn to use scope) to control when the delete should occur.


Summary:

stack vs heap vs size of instance

life span using scope controls

2785528
  • 5,438
  • 2
  • 18
  • 20
-1

Tower *createTower(string name, Tower *link); THIS IS USED WHEN YOU ARE RETURNING A TOWER(MEANS A STRUCTURE) IN YOUR FUNCTION NAMED createTower(THIS IS A FUNCTION POINTER WHICH WILL RETURN A STRUCTURE)

Kunal Saini
  • 138
  • 10
  • From what I'm learning, this would not be a function pointer. The function pointer equivalent should be - `Tower* (*createTower)(string name, Tower *link)` Correct me if I'm wrong – greeneyedguy May 29 '15 at 13:50