C语言中swap的作用和用法

#include<stdio.h>void main() { void swap(int * p1,int * p2; int a,b; int * pointer_1,* pointer_2; scanf("%d,%d",&a,&b); pointer_1=&a;pointer_2=&b if(a<b) swap(pointer_1,pointer_2); printf("\n%d,%d\n",a,b); }void swap(int *p1,int *p2) 输入 5,9 输出9,5 {int temp; temp=*p1; *p1=*p2 ; *p2=temp;

swap函数一般是一个程序员自定义函数。

    通常是实现两个变量数值的交换,用法比较广泛。

    可使用临时变量实现交换;可通过临时指针变量实现交换;可借助指针加入临时变量来实现交换。

    return 0;

    }

    swap1: x:4,y:3

    swap2: x:4,y:3

    swap3: x:3,y:4

    swap4: x:4,y:3

    swap5: x:3,y:4

    swap6: x:4,y:3

    swap7:ppx:3,ppy:4

拓展资料:

计算机中swap函数很多,在不同领域有不同的用法,但都是交换的意思。比如字符串交换swap操作实现交换两个容器内所有元素的功能。要交换的容器的类型必须匹配: 必须是相同类型的容器,而且所存储的元素类型也必须相同。调用了swap函数后,右操作数原来存储的元素被存放在左操作数中,反之亦然。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-06-02

1.作用:swap的意思是交换两个变量的值,是一个自定义函数。

2.用法使a和b的值进行互换。

例如:void swap(int*p1,int*p2)    //*p1=a;*p2=b;

改变指针指向的地址的值,即a和b的值互换。

3.其他用法

swap1只进行了值传递,所以函数调用结束后形参被释放,不能实现实参的值交换;

swap2直接使用全局变量,这样swap2函数和main函数操作的是同一个变量(地址和值都一样),可以实现值交换;

swap3使用传地址的方式,通过修改内存块来实现变量的值交换,是可以的。

swap4使用引用(&)的方式,这样是给mian函数中待交换的变量起一个别名,并把把别名作为形参在swap4中进行处理,这其实就实现了形参和实参的地址和内容完全一样,当然可以实现值交换,swap4的效果和swap2的一样,但这种定义方式更利于程序的调试和维护,同时也可以减小内存开销。

swap5中虽然也把变量的地址传到了函数中,但在函数内部并没用修改地址指向的内存块而是把地址在形参上完成交换,swap5函数运行结束,所有的工作都会都是,而main函数中的变量也没有实现交换,这种情况和swap1类似。

具体代码如下:

    /*-----try to swap the value of a and b, but it does not work out.*/  

    /*void swap1(int x,int y) 

    int temp; 

    temp = x; 

    x = y; 

    y = temp; 

    }*/  

    /*------using the global variables can implement the swap----*/  

    /*int a(3),b(5); 

    //the declarations of a and b in the main function should be commented out. 

    void swap2() 

    int temp; 

    temp = a; 

    a = b; 

    b = temp; 

    }*/  

    /*----using the pointer to pass the address to the swap function*/  

    /*void swap3(int *px,int *py) 

    int temp; 

    temp = *px; 

    *px = *py; 

    *py = temp; 

    }*/  

    /*----using the reference operator(&)-----*/  

    void swap4(int &x, int &y)  

    {  

    int temp;  

    temp = x;  

    x = y;  

    y = temp;  

    }  

    /*----meaningless swap---*/  

    /*void swap5(int *px,int *py) 

    int *p; 

    p = px; 

    px = py; 

    px = p; 

    }*/  

    int main(int argc, char* argv[])  

    {  

    int a(3),b(5);  

    printf("before swap:%3d %3d\n",a,b);  

    swap4(a,b);  

    printf("after swap:%3d %3d\n",a,b);  

    return 0;  

    }  

本回答被网友采纳
第2个回答  2018-05-24

函数名: swab 

功  能: 交换字节 

用  法: void swab (char *from, char *to, int nbytes); 

程序例: 

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

char source[15] = "rFna koBlrna d"; 
char target[15]; 

int main(void) 
{
swab(source, target, strlen(source));
printf("This is target: %s\n", target);
return 0; 

拓展资料:

C语言字符串函数大全:

函数名: stpcpy 

功 能: 拷贝一个字符串到另一个 

用 法: char *stpcpy(char *destin, char *source);

函数名: strcat

功 能: 字符串拼接函数

用 法: char *strcat(char *destin, char *source);

函数名: strchr

功 能: 在一个串中查找给定字符的第一个匹配之处

用 法: char *strchr(char *str, char c);

函数名: strcmp

功 能: 串比较

用 法: int strcmp(char *str1, char *str2);

函数名: strncmpi

功 能: 将一个串中的一部分与另一个串比较, 不管大小写

用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);

函数名: strcpy

功 能: 串拷贝

用 法: char *strcpy(char *str1, char *str2);

函数名: strcspn

功 能: 在串中查找第一个给定字符集内容的段

用 法: int strcspn(char *str1, char *str2);

函数名: strdup

功 能: 将串拷贝到新建的位置处

用 法: char *strdup(char *str);

第3个回答  2021-04-06
C语言中swap的作用和用法?swap函数一般是一个程序员自定义函数。
1.
通常是实现两个变量数值的交换,用法比较广泛。
2.
可使用临时变量实现交换;可通过临时指针变量实现交换;可借助指针加入临时变量来实现交换。
3.
return 0;
4.
}
5.
swap1: x:4,y:3
6.
swap2: x:4,y:3
7.
swap3: x:3,y:4
8.
swap4: x:4,y:3
9.
swap5: x:3,y:4
10.
swap6: x:4,y:3
11.
swap7:ppx:3,ppy:4
拓展资料:
计算机中swap函数很多,在不同领域有不同的用法,但都是交换的意思。比如字符串交换swap操作实现交换两个容器内所有元素的功能。要交换的容器的类型必须匹配: 必须是相同类型的容器,而且所存储的元素类型也必须相同。调用了swap函数后,右操作数原来存储的元素被存放在左操作数中,反之亦然。本回答被网友采纳
第4个回答  2015-05-26

  swap函数一般是一个程序员自定义函数。通常是实现两个变量数值的交换。比如

int a = 2;
int b =3;
swap(a,b); //一般用到变量数值交换,交换后a=3 b = 2;

  实现的方法多种多样。比如下面几种写法:

  1、通过使用临时变量实现交换。

  void swap1(int x,int y)
 {        
    int temp;          
    temp=x;
    x=y;
    y=temp;
   }

  2、通过临时指针变量实现交换。

 void swap2(int *x,int *y)
    {
    int *temp;
    temp=x;
    x=y;
    y=temp;
    }

  3、借助指针加入临时变量来实现交换。

   void swap3(int *x,int *y)
    {
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
    }

本回答被网友采纳
相似回答