# 交换数组元素

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/22406.html | 发布：2023-07-25 | 更新：2023-07-25 | 分类：建站教程

## 交互数组元素

 

例题：

 

使用下面的代码来交换数组中两个元素的位置：

 

1：void swap(int *a, int *b):

 

2：{  
3： int temp = *a;  
4：*a = *b;  
5：L *b = temp;  
6：}  
方法：

 

1：int arr[] = {1, 2, 3, 4, 5};

 

2：swap(&amp;arr[1], &amp;arr[3]); // 交换 arr[1] 和 arr[3] 的值:

 

此例中，数组 arr 的值变成了 {1, 4, 3, 2, 5}。

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
