有两个数a,b,请写一个函数交换a,b

时间:2022-11-28 09:53:30

题目:有两个数a,b,请写一个函数交换a,b,具体实现如下:

#include<stdlib.h>
#include<stdio.h>
int swap(int * pA, int * pB)
{
if (pA == NULL || pB == NULL)
{
return 0;
}
int nTemp = *pA;
*pA = *pB;
*pB = nTemp;
return 1;
} int main()
{
int a = 3;
int b = 5;
printf("交换前:a:%d,b:%d\n", a, b);
if (!swap(&a, &b))
{
printf("交换失败.\n");
return 0;
}
printf("交换后:a:%d,b:%d\n", a, b);
system("pause");
return 0;
}

运行效果如图1所示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqUAAABbCAIAAADFvWslAAAO30lEQVR4nO2d7W8cRx3H/efcH3Dv/dLCCDjUGohQdQarUoMQEjpRCQQ6AUJ9CG1p6wonEEpT2kahtFyuUZOmSc6uoKYPUm3q0hABdZHaivgpTmgS+sK8uLu92Xn47ex672a99/noq+i8Nzs7uz7vZ2Z2bzNx9miNEEIIIeXOxNmjtZ3lw4QQQgjJnNdrtSLE1Tx8TwghhOSQ4KbH94QQQsjQE9z0+J4QQggZeoKbHt8TQgghQ09w0+N7QgghZOgJbnp8TwghhKRIRcS1VnDT43tCCCEkRUbs+729vb29PXnJ6Hy//Md7zz37fZ+8/uL3tv58T/DfFiGEEJItgtSHNL5XBb8f2efg++dfPP3WO2tvvfOukbW3V9beXlk7fW7p9NnF02cXnz31wr/OfSP4b4sUJQuTlanpVWeBmYbr3dZ0rTLZXpisVCbbvYX1+al4N3tupj1n6X3XmvXB1ger97eoFnBktVmtVKrzrdBHr+CHtyxpz1XEw0jGLqP3faT5fco+B99f7Czeun375q2E3Lp9+/zFxfdfKtvpYNyy2qzGz/Au8800KpXGglibTUjtuUGFzlNtV0ixGurzU4PNrTarlbmZfvn6/JTZSENglcmGKbCp6dXlmYY0ezdYvS3s8sKkxYit6ZqlnqipsRZqHlV/C86DHObwhj0I9q17f2gtGYLvE/80unutdUZJUVIJ4fv9j+zz8f3i0tL7f//3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