Hi all,
When I was compiling this code in Linux with g++, I got the Segmentation Fault. I guess I might have
misused the pointer in my function. Could you help me figure out what's wrong with my code?
What this code should do is swapping the values of int a and int b with function swap...
Code:#include <iostream> using std::cin; using std::cout; using std::endl; void swap(int *p, int *q) { int *temp; *temp=*p; *p=*q; *q=*temp; } int main() { int a=5,b=10; int *p=&a; int *q=&b; swap(p,q); cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; return 0; }




Reply With Quote
