BoeNoe BoeNoe - 13 days ago 5
C++ Question

Asterisk in function type

I found some code online for a C++ Chat Server. And in the code below contains something I don't get

string* buildPrompt()
{
// Code for chat server
}


What is that asterisk after
string
? If I remove it, will the code stop working?

Answer

Asterisk after the string means that it is a pointer to a string object somewhere else in memory. Removing the asterisk changes the return type from the function:

string* buildPrompt();  // <-- Returns pointer to a string.
string buildPrompt();   // <-- Returns an instance of a string.

I would recommend doing some basic reading on pointers in C++, they're pretty fundamental to the language once you advance from simple "Hello, World!" programs.

https://en.wikipedia.org/wiki/Pointer_(computer_programming)