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().

7th Oct 2024, 8:40 PM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
4 odpowiedzi
+ 3
Sounds like that's your homework. Show us what you've got so far.
7th Oct 2024, 11:44 PM
Jerry Hobby
Jerry Hobby - avatar
+ 3
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; }
8th Oct 2024, 4:49 AM
Bob_Li
Bob_Li - avatar
+ 1
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; }
8th Oct 2024, 1:07 AM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
0
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.
8th Oct 2024, 2:15 PM
Jerry Hobby
Jerry Hobby - avatar