【C语言及程序设计】指针与返回值

指针与返回值(赋值)是两种自定义函数对主函数内变量的操作手段

例:

#include "stdafx.h"

void exchange(int *, int *, int *);
void myswap(int *, int *);

int main()
{
	int a, b, c;
	scanf_s("%d %d %d", &a, &b, &c);
	exchange(&a, &b, &c);   //另一种调用方法
	printf("%d %d %d\n", a, b, c);
	return 0;
}

void exchange(int *q1, int *q2, int *q3)
{
	// 1 2 3
	if (*q1<*q2) myswap(q1, q2); // q1-&a-b-2, q2-&b-a-1 
	if (*q1<*q3) myswap(q1, q3); // q1-&a-c-3, q3-&c-b-2
	if (*q2<*q3) myswap(q2, q3); // q2-&b-b-2, q3-&c-a-1 》&a-c-3 &b-b-2 &c-a-1
}

void myswap(int *pt1, int *pt2)
{
	int temp;
	temp = *pt1; //p1-q1-&a p2-q2-&b t-a
	*pt1 = *pt2; //p1-&a-b p2-&b-b t-a
	*pt2 = temp; //p1-&a-b p2-&b-a t-a
}

  

=

猜你喜欢

转载自www.cnblogs.com/miyazakehime/p/9249839.html