0
Swapping two numbers in c programming
Swapping Values Passing pointers to functions is a great way to solve some challenging tasks. One example is swapping values. You have two variables values, which are taken from input, and you need to swap the values. The given code declares two variables, takes them from input and passes them to a swap() function, as pointers. The swap() function needs to swap their values. Task: Task is the define the swap() function so that the given code works as expected. Do not change the outputs in main().
8 Respuestas
+ 5
Prakash Kumar Jaiswal
your swap should be a void function, since you are working with pointers and passing by reference. No need for the return value, which you did not use in the actual function call.
void swap(int *a, int *b){
int c = *a;
*a = *b;
*b = c;
}
+ 3
Sounds like that's your homework. Show us what you've got so far.
+ 2
Yup that's right! Jerry Hobby
And I already solved it but the problem is still confusion about uses of pointers in functions & all.
//Swapping Values
#include <stdio.h>
//create the swap() function here
int swap(int *a, int *b){
int c = *a;
*a=*b;
return (*b=c);
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
printf("x is %d, y is %d\n", x, y);
swap(&x, &y);
printf("x is %d, y is %d\n", x, y);
return 0;
}
+ 2
Data storage can be complicated to understand. In short ...
Variables have two parts.
- The memory location where the data is stored. That is called the reference.
- The value that is stored at the location.
Whenever you create a variable, it has a value, as you know. But where is that value actually stored in memory? That's the reference.
Every time you pass a variable to a function, you are passing a copy. You can pass a copy of the value or pass a copy of the reference. Because whatever you pass is actually a copy, you can safely pass values to a function and know that the original data is left unaltered in the parent function. The default is to pass by value, so passing data to a function is by default non-destructive to the original variables.
If you want the function to actually modify the data, you need to pass the reference (address of). That enables the function to know exactly where the values were stored in memory. That makes it possible to update the values at the precise physical location where those values are stored.
Knowing when to pass by value or pass by reference depends if you want the function to actually update the values directly or not.
In your code, the function needs to update the values in the original locations. So instead of passing the values, which would only be a copy, you need to pass the reference. Then the function can update the variables in their original memory locations.
The two things you have to wrap your head around:
- Every variable has a REFERENCE (memory address) and a VALUE
- Every argument to a function is a copy
Here's some code to demonstrate that:
https://www.sololearn.com/en/compiler-playground/cTm30ZjyzVRb
In your homework, you want your function to exchange the values at the original memory locations, so to accomplish that, you have to pass the address of the variables so the function can find and modify the variables in the original location.
It may take a bit for all this to make sense. I hope this helps at least a little.
+ 1
Bob_Li ,
Thanks for your valuable suggestions.
But there is in the hint comment we must include the return value in the function call that's why I am using that return type function call.
Sorry for not mentioning that hint comment in my question.
+ 1
Prakash Kumar Jaiswal
You might have misread the task description. There is nothing here that says you have to use the return keyword.
"
Swapping Values
Passing pointers to functions is a great way to solve some challenging tasks.
One example is swapping values. You have two variables values, which are taken from input, and you need to swap the values.
The given code declares two variables, takes them from input and passes them to a swap() function, as pointers. The swap() function needs to swap their values.
Task:
Define the swap() function so that the given code works as expected. Do not change the outputs in main() .
Hint: In the swap() function, create a third variable and use it to store and swap values of x and y. Do not forget to assign the new values to the actual variables, using the pointers, so that they are changed in main().
"
using pointers to assign values gives you the convenience of not having to return them.
The lesson is right after functions, so you might have mixed them up
+ 1
it is correct
#include <stdio.h>
//create the swap() function here
int swap(int *a, int *b){
int c = *a;
*a=*b;
return (*b=c);
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
printf("x is %d, y is %d\n", x, y);
swap(&x, &y);
printf("x is %d, y is %d\n", x, y);
return 0;
}
0
Jerry Hobby ,
Thank you for your valuable intuitive information about functions.
Once again thanks, it helps me a lot. 🙏