Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:11:27+00:00 2026-06-07T14:11:27+00:00

Possible Duplicate: C++ functions: ampersand vs asterisk What are the distinctions between the various

  • 0

Possible Duplicate:
C++ functions: ampersand vs asterisk
What are the distinctions between the various symbols (*,&, etc) combined with parameters?

I am wondering the difference between the address operator & and the deference operator * in a C++ function call. For example take the following function

void foo (std::string& param)
{
     param = "Bar.";
     std::cout << param.size();
}

and let’s call it in our main() function like so…

int main()
{
      std::string test;
      foo(test);          //Why not foo(&test)?
      std::cout << test;  //Prints out the value "Bar."
}

First off, why does the & operator allow me to assign a value as if it were a pointer (assigning it a value that survives the RAII and scope of the function foo() when it’s not a pointer) as it is able to be printed out in my main() function even though it’s not static? I am assuming it is not a pointer because I am able to access the size() method by using the . operator instead of the -> which is used for pointers.

Secondly, what would be the difference between using the & operator in a function parameter vs. using the * operator? Is it even different than just a plain variable like std::string param? It appears to be called like that (foo(test) instead of foo(&test)).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:11:29+00:00Added an answer on June 7, 2026 at 2:11 pm

    & function parameter specifically signifies that this parameter is being passed-in by reference (traditionally compilers implement this as a pointer) which is why you see the effect of this assignment in your main().
    static would have nothing to do with that.

    The difference in declaring a parameter to a function using & and * is that the second one allows a nullptr (or a non-existent or just a plain invalid address) to be passed-in while the & guarantees that there’s a real object being referenced by this function’s argument. Other than that both provide similar functionality of allowing an original object to be changed via it’s reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Performance difference between functions and pattern matching in Mathematica I often find
Possible Duplicate: Functions vs methods in Scala What is the difference between def foo
Possible Duplicate: What is the difference between these jQuery ready functions? $(function(){ // ...
Possible Duplicate: The difference between the two functions? (“function x” vs “var x =
Possible Duplicate: jQuery delay between animations I have created two functions: One that animates
Possible Duplicate: How to pass objects to functions in C++? Operator & and *
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: How do you trigger javascript functions from flash? Possible Duplicate: How do
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.