+ 1
Doubt in pointers in C programming
I don't know how does the below code,swap the two numbers? (i.e how does swaping pointer variable value,swap the numbers? ) Please clear my doubt https://code.sololearn.com/c4g4iJ8lOCrv/?ref=app
9 Réponses
+ 4
Vishal Pandey
I can't understand your answer. First of all, C does not have references. Did you mean 'pointers'? Secondly, you say that what the OP has done is too complex. But you have done the same thing in your answer.
Yogeshwaran
"I can't able to clearly understand how does swapping address swap the values"
The addresses are not being swapped. The values in those addresses are
`int temp = *a;`
Here you are putting the value stored in the *address stored inside 'a'* into 'temp'
`*a = *b`
Here, you are putting the *value pointed to by 'b'* into the *value at address stored in 'b'*
`*b = temp;`
Here, you are putting the value of 'temp' in the *value at address stored in 'a'*
+ 3
How normal variables swapping is done:
int temp = a;
a = b;
b = temp;
How in pointer it is done:
int *aptr = &a;
int *bptr = &b;
int temp = *aptr;
*aptr = *bptr;
*bptr = temp;
You see, they look a lot similar, especially in pointer version the 3rd to 5th line almost looks the same as in normal way. In the pointer version we just have to assign them the addresses before swapping.
Also, *aptr IS a, and ptr IS &a. What *aptr do also apply to a instantly since *aptr is a.
+ 1
Why you have done too complex in swapping with pointers.
Remember & is used for reference. I think that you know about call by value and call by reference. If you give & in any function it takes from the main function as the call by reference and if you dont use & it takes call by value.
#include<stdio.h>
void swap(int *x,int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int n1,n2;
scanf("%d%d\n",&n1,&n2);
swap(n1,n2);
//swap(&n1,&n2);
printf("%d%d\n",n1,n2);
return 0;
}
May it help you think it a twice.
Again & it uses as a reference to pass the actual value from that variable or function..
+ 1
The core of a pointer, as its heart, is to store an address. An address is a location. In C it refers to where the variable actually is in the computer.
So how do we get an address of a variable? All we need is this '&'. Adding this before a variable will return its address (its location).
int a = 5;
&a //get a's address.
Now we know how to get the address. We then can assign it into the pointer and thus:
int *ptr = &a;
(Note: just in case you want to check the address yourself, you can print the address of a variable with printf("%p", &a);. The %p is for the address, like %d for int, %c for char, etc. Though often time or basically most of the time you will still have a happy life without knowing the address.)
+ 1
Thank you Martin Taylor for yours neat and clear explanation with example ....(•‿•)
This makes me clear to understand...(^‿^)
Once again thanks for all, who answered my question by spending yours valuable time.....(✿^‿^)
0
Vishal Pandey , actually ,I have a doubt on, working of my whole code....(including yours code too)
I can't able to clearly understand how does swaping address ,swap the values?
Please guide me step by step ,what is actually happening on both of ours code 😅
0
CarrieForle can you please give me detailed answer?😅
From the address level, variable assigning and swapping..
Because this is my actual doubt on that code
0
XXX can you please give your answer in the coding format?
Which makes to understood your answer more easier..
Because still I can't caught your answer on
*a = *b and *b = temp ?😔
Sorry for inconvenience