Write your question here.
Why does the program work onle when i remove the * before c on line 9, i don't understand how does it make sense under this circumstance, plz help. Thank you ver much
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Put the code you need help with here.
#include <iostream>
usingnamespace std;
int swap(int **a, int **b){
int *c=*a;
*a=*b;
*b=*c;
}
int main(){
int x = 5, y = 6;
int *ptr1 = &x , *ptr2 = &y;
swap(&ptr1, &ptr2);
cout << *ptr1 << " " << *ptr2 ; // Prints "6 5"
}
b is type pointer-to(pointer-to-int)
c is type pointer-to-int
This makes *b type pointer-to-int
*c type int
When you try *b = *c you're trying to assign an int type to a pointer-to-int type, and you get a type mismatch error. Here's a concrete example:
1 2 3 4 5 6
int **a, **b; //both are type pointer-to(pointer-to-int)
int *c; //c is type pointer-to-int
*c = 10; //okay
*a = c; //okay, both are type pointer-to-int
*b = *c; //not okay, *b is type pointer-to-int and *c is type int
*b = 10 //also not okay, same reason as above