Simple C swap function

0 votes

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses?

It does not work.

void num_exchange(int m, int n);

int main(){

int num1 = 5;
int num2 = 6;

num_exchange(num1 , num2 );

cout << "num1 =" << num1 << endl;
cout << "num2 =" << num2 << endl;

return 0;
}

void num_exchange(int m, int n){
int temp;
temp = m;
m = n;
n = temp;
}

Works

void arr_exchange(int [], int);

int main(){


int n[7] = { 0, 0, 0, 0, 0, 0, 0 };

arr_exchange(n, 7);
for (int i = 0; i < 7; i++)
    cout << n[i] << " ";

return 0;
}

void arr_exchange(int x[], int){
for (int i = 0; i < 7; i++)
    x[i] = 1;
}

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
449 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
0 answers

How to use c++ swap function?

It is OK to write swap(a,b); There is an issue when I write swap(&c[0],&d[0]);.  Can anyone explain why? #include<iostream> #include<algorithm> using namespace std; int main(void){ ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
334 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
518 views
0 votes
1 answer

Simple linked list in C++

This is the most basic example I can think of in this situation, and it has not been tested.  Please keep in mind that this violates some C++ best practises and deviates from the norm (initialize lists, separation of declaration and definition, and so on).  But those aren't topics I can discuss here. #include <iostream> using namespace std; class LinkedList{ ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
863 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
594 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,514 views
0 votes
1 answer

please help me with max_element function in c++ stl

You can substitute max for *max eleme ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
712 views
0 votes
1 answer

Passing a 2D array to a C++ function

There are three ways to pass a ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
30,415 views
0 votes
1 answer

How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

answered Aug 5, 2022 in C++ by Damon
• 4,960 points
952 views
0 votes
0 answers

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE

Aug 5, 2022 in C++ by krishna
• 2,820 points
943 views
0 votes
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
470 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP