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 7525917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:41:15+00:00 2026-05-30T03:41:15+00:00

void swap(int a[], int size){ …………… } int main(){ int x[4] = {4,5,3,12} ;

  • 0
void swap(int a[], int size){
   ...............
}

int main(){
   int x[4] = {4,5,3,12} ;
   swap(x,4) ;
   // print array here - some elements are swapped
}

So I’m not sure how it is possible for the swap-function to change the order of elements
since it is passed by value in void swap?

If I do something like this: void swap( int (&a)[], int size)
then the original array in my main function would get changed, but how does swap function swap elements, if it just copies the array, and hence should not make any effect on the array in main?

  • 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-05-30T03:41:18+00:00Added an answer on May 30, 2026 at 3:41 am

    In C and C++, when you pass an array to a function, you are actually only passing its address (yes, by value, but of course that doesn’t change anything since an address passed by value or not always points to the same location).

    In the function, whatever element of the array you access or change would be the original one.

    So basically, your answer is that you got everything right and no need to do anything.

    Example:

    void swap(int a[], int size){
        for (int i = 0; i < size/2; ++i)
        {
            a[i] ^= a[size-1-i];   // If you are interested
            a[size-1-i] ^= a[i];   // try to understand
            a[i] ^= a[size-1-i];   // how this works
        }
    }
    
    int main(){
       int x[4] = {4,5,3,12} ;
       swap(x,4) ;
       // print array here - some elements are swapped
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In some comp-sci papers and tests, I see swap() implemented like so: void swap(int
#include stdio.h #include conio.h void swap(int *x,int *y); void main() { int a=10,b=20; swap(a,b);
I am studying pointers and I encountered this program: #include <stdio.h> void swap(int *,int
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
typedef unsigned char Byte; ... void ReverseBytes( void *start, int size ) { Byte
public class Main { public static void main (String args[]) { int nums[]= {2,
public void PlayforMe() { for (int i=game.size()-1;i>=1;i--) { Card a = game.get(i); Card b
void swap(int &first, int &second){ int temp = first; first = second; second =
void swap(ref int x, ref int y) { x = x ^ y; y
Does a method like this exist anywhere in the framework? public static void Swap<T>(ref

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.