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

  • SEARCH
  • Home
  • 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 52205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:52:13+00:00 2026-05-10T16:52:13+00:00

In the following code, both amp_swap() and star_swap() seems to be doing the same

  • 0

In the following code, both amp_swap() and star_swap() seems to be doing the same thing. So why will someone prefer to use one over the other? Which one is the preferred notation and why? Or is it just a matter of taste?

#include <iostream>  using namespace std;  void amp_swap(int &x, int &y) {     int temp = x;     x = y;     y = temp; }  void star_swap(int *x, int *y) {     int temp = *x;     *x = *y;     *y = temp; }  int main() {     int a = 10, b = 20;     cout << 'Using amp_swap(): ' << endl;     amp_swap(a, b);     cout << 'a = ' << a << ', b = ' << b << endl;     cout << 'Using star_swap(): ' << endl;     star_swap(&a, &b);     cout << 'a = ' << a << ', b = ' << b << endl;     return 0; } 

Thanks for your time!


See Also

Difference between pointer variable and reference variable in C++

  • 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. 2026-05-10T16:52:14+00:00Added an answer on May 10, 2026 at 4:52 pm

    One is using a reference, one is using a pointer.

    I would use the one with references, because you can’t pass a NULL reference (whereas you can pass a NULL pointer).

    So if you do:

    star_swap(NULL, NULL); 

    Your application will crash. Whereas if you try:

    amp_swap(NULL, NULL); // This won't compile 

    Always go with references unless you’ve got a good reason to use a pointer.

    See this link: http://www.google.co.uk/search?q=references+vs+pointers

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

Sidebar

Related Questions

I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
In the following code, will freeing Dairy free Yogurt as well? Both point to
I am using the following code to update my mysql table where both moving50
I'm doing a chat for my social network. I'm using the following code and
I have the following code. dateStart & dateEnd are both DateTime. My issue is
The following code produces random values for both n and v. It's not surprising
With the following code I am trying to set both the test title and
The following code compile well both with GCC (4.2-4.6) and with Clang (2.1), but
The following code contains a potential deadlock, but seems to be necessary: to safely
The following code will not run correctly in IE7 with the latest service packs

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.