#include <stdio.h>
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int a=10;
int b=20;
printf ("%d%d",a,b);
swap(a,b);
printf ("%d%d",a,b);
}
输出结果:10 20 10 20
本意是要在子函数中交换a,b的值,但并没有达到想要的目的。
#include <stdio.h>
void swap(int *p,int *q)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
输出结果:10 20 20 10{
int a=10;
int b=20;
printf ("%d%d",a,b);
swap(&a,&b);
printf ("%d%d",a,b);
}
void swap(int *p,int *q)
{
int *temp; //野指针,悬挂指针,没有有效的地址
*temp=*p; //崩溃
*q=*temp;
}
int main()
{
int t=10;
int *p=&t;
p=NULL; //空指针,无效的指针
printf("%d",*p); //程序崩溃,意料之中
return 0;
}