0

When I invoke swap function, code run properly but when invoke _swap function , it show error

#include <stdio.h> void swap(int a,int b); void _swap(int*a,int*b); int main() { int x = 4,y = 9; _swap(x,y); printf("x = %d & y = %d \n",x,y); return 0; } void swap(int a,int b){ int t = a; a = b; b = t; printf("a = %d & b = %d\n",a,b); } void _swap(int*a,int*b){ int t = *a; *a = *b; *b = t; printf("*a = %d & *b = %d\n",*a,*b); }

21st Oct 2022, 12:01 PM
Munna Raj
Munna Raj - avatar
2 odpowiedzi
+ 1
Shouldnt you invoke it like _swap(&x, &y); ???
21st Oct 2022, 12:34 PM
arturop200
arturop200 - avatar
0
Pointers are used to store address of other variables. Storing direct values is useless and invalid.. So pass address rather than values.
21st Oct 2022, 12:52 PM
Jayakrishna 🇮🇳