When I invoke swap function, code run properly but when invoke _swap function , it show error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answers
+ 1
Shouldnt you invoke it like _swap(&x, &y); ???
21st Oct 2022, 12:34 PM
Arturop
Arturop - 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 🇮🇳