首页 技术 正文
技术 2022年11月11日
0 收藏 315 点赞 4,787 浏览 991 个字
#include<stdio.h>  //头文件
main() //主函数
{
void swap(int *p,int *q); //声明
int a,b; //定义两个整数
int *p1,*p2; // 定义两个地址
scanf("%d,%d",&a,&b); //输入两个整数
p1=&a; //p1指向a
p2=&b; // p2指向 b
swap(p1,p2); //交换p1和p2
printf("%d,%d\n",a,b); //输出a,b的值
}
void swap(int *p,int *q) //调用函数
{
int k; //定义一个整数
k=*p; // 将*p的值赋给k
*p=*q; //将*q的值赋给*p
*q=k; //将k的值赋给*q
}
1,2
2,1--------------------------------
Process exited after 14.19 seconds with return value 0
请按任意键继续. . .

总结:swap函数用的不熟练,定义函数时总出错。

#include<stdio.h>void swap(int *p,int *q)
{
int *m;
*m=*p;
*p=*q;
*q=*m;
}

指针变量在使用的时候没有进行初始化,所以有可能指向是其他重要的数据。

#include<stdio.h>void swap(int *p,int *q)
{
int *m;
m=p;
p=q;
q=m;
}

在swap函数中只是对指针变量中的地址进行调换(&p和&q交换),而并没有影响到a,b的值。

#include<stdio.h>
int main()
{int p1,p2;//定义整型
int*p,*q,*c;//定义指针
p=&p1;//p指向i
q=&p2;//q指向j
int*swap(int*a,int*b);//定义函数指针 ,让函数返回指针类型数据
scanf("%d,%d",p,q);
c=swap(p,q);//将函数的返回值赋值给指针变量c
printf("%d",*c);
return ;
}
int*swap(int*a,int*b)
{if(*a<*b)
return b;//返回指针变量
else
return a;
}
1,2
2
--------------------------------
Process exited after 3.378 seconds with return value 0
请按任意键继续. . .

总结:指针运用是需要注意何时用&,注意将p指向i和q指向j。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287