关于调用函数过程中被调用函数的问题

youxirenjia90 2016-05-18 10:42:16

#include <stdio.h>

swap(int *p1,int *p2) /*swap(int x,int y); /*swap(int *p1,int*p2)
int temp; int *p;
temp=x; p=p1;
x=y; p1=p2;
y=temp;*/ p2=p; */
{int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int main()
{
int a,b;

int *pointer_1;
int *pointer_2;

scanf("%d%d",&a,&b);

pointer_1=&a;
pointer_2=&b;
if(a<b)
{
swap(pointer_1,pointer_2);
printf("%d\n%d\n",a,b);
}
return 0;
}
以上过程中被调用函数swap有3种定义方法,加注释/*..*/内的定义不能正常被调用,我想不明白的是注释内的定义过程有什么问题么,请各位大神指教,谢谢
...全文
186 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghx287524027 2016-05-19
  • 打赏
  • 举报
回复
引用 4 楼 youxirenjia90 的回复:
[quote=引用 2 楼 ghx287524027 的回复:] 值传递和引用传递的问题
你好,这两个有什么具体的区别么。对于值传递和引用传递?[/quote] 关于值传递和引用传递,百度上有很多,c/c++/java都可以,原理差不多
赵4老师 2016-05-19
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0)
char   *p1="1" ,*p2="2" ;
char    c1=1   , c2=2   ;
short   s1=1   , s2=2   ;
int     i1=1   , i2=2   ;
__int64 I1=1i64, I2=2i64;
float   f1=1.0f, f2=2.0f;
double  d1=1.0 , d2=2.0 ;
void main() {
    SWAP((int)p1,(int)p2);                printf("char *     %5s,   %5s\n",p1,p2);
    SWAP(c1,c2);                          printf("char       %5d,   %5d\n",c1,c2);
    SWAP(s1,s2);                          printf("short      %5d,   %5d\n",s1,s2);
    SWAP(i1,i2);                          printf("int        %5d,   %5d\n",i1,i2);
    SWAP(I1,I2);                          printf("__int64 %5I64d,%5I64d\n",I1,I2);
    SWAP(*(int     *)&f1,*(int     *)&f2);printf("float      %5g,   %5g\n",f1,f2);
    SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double    %5lg,  %5lg\n",d1,d2);

    SWAP(c1,c1);
    printf("%d\n",c1);
}
//char *         2,       1
//char           2,       1
//short          2,       1
//int            2,       1
//__int64     2,    1
//float          2,       1
//double        2,      1
//2
paschen 2016-05-19
  • 打赏
  • 举报
回复
你函数中的参数只是实际参数的一个复制器,你交换的仅仅是两个复制品,而不是实际参数 用指针的话,指针是个复制器,但你可以修改指针指向的值
lm_whales 2016-05-19
  • 打赏
  • 举报
回复
C 只有值传递 所以,需要改变的对象要传入地址。形参是指针,通过指针间接改变 ,传入地址中的数据(这是指针的功能)。 C++多了个引用参数 传入对象(名),即可匹配引用参数 引用参数可以修改传入的实参对象。(这就是别名的含义)
ghx287524027 2016-05-19
  • 打赏
  • 举报
回复
姑且这么标识一下

#include <stdio.h>

swap(int *p1,int *p2){
	int temp;              
	temp=*p1;                 
	*p1=*p2;
	*p2=temp;
}
swap1(int x,int y){    
	int temp;
	temp=x;
	x=y; 
	y=temp;
}
swap2(int *p1,int*p2){
	int *p;
	p=p1;
	p1=p2;
	p2=p;
}

int main(){
	int a,b;
	int *pointer_1;
	int *pointer_2;
	scanf("%d%d",&a,&b);
	pointer_1=&a;
	pointer_2=&b;
	if(a<b){
		swap(pointer_1,pointer_2);
		swap1(*pointer_1,*pointer_2);
		swap2(pointer_1,pointer_2);
		printf("%d\n%d\n",a,b);
	}
	return 0;
} 
swap属于引用传递,可以达到交换a,b的目的,不必多说。 swap1属于值传递,虽然对x,y进行了交换,但跟a,b没关系,不会影响a,b的值。 swap2虽然也是引用传递,但是只是改变了指针的指向,a,b的实际值并没有改变
youxirenjia90 2016-05-19
  • 打赏
  • 举报
回复
引用 2 楼 ghx287524027 的回复:
值传递和引用传递的问题
你好,这两个有什么具体的区别么。对于值传递和引用传递?
ghx287524027 2016-05-19
  • 打赏
  • 举报
回复
值传递和引用传递的问题
用户 昵称 2016-05-19
  • 打赏
  • 举报
回复
这就是指针嘛,/*.........*/中的代码,说白了就是 a = 3; b = 4; swap( a, b );实际上就是swap( 3, 4 ) 3和4都是数,怎么可能被交换呢? 换成指针就是 swap( 装3的盒子, 装4的盒子 ),在swap里面把盒子里面的东西互换一下。
ghx287524027 2016-05-19
  • 打赏
  • 举报
回复
能不能规范一下代码啊,看起来很痛苦的说~

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧