I know the & operator means address-of, but what's it do when in a function prototype?

Example:

void foo(std::string& str)

or

void foo(char& c)

what's the & for in that?

I know the & operator means address-of, but what's it do when in a function prototype?

Example:

void foo(std::string& str)

or

void foo(char& c)

what's the & for in that?

The & symbol means that you are passing the parameter to the function by "reference" rather than by "value". When the & symbol is next to a parameter, if you change that parameter in the function, that will change the variable that was passed to it when the function was called.

For example,

void foo(char& c)
{
     c = 'a';
}

If the code below was executed:

char x;
foo (x);
cout << x;

the output would be this:

a
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.