> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c01/ft_swap.md).

# ft\_swap

**Objective:**

Create a function that swaps the values of two integers using their addresses (pointers).

**Turn-in Requirements:**

* **Directory**: `ex02/`
* **File**: `ft_swap.c`
* **Allowed Functions**: None

**Prototype:**

```c
void ft_swap(int *a, int *b);
```

**Implementation:**

Here’s how you can implement the function:

```c
// File: ft_swap.c
#include <unistd.h> // Optional, not needed for this task but can be standard practice for headers

void ft_swap(int *a, int *b)
{
    int temp;

    temp = *a;  // Store the value of *a in a temporary variable
    *a = *b;    // Assign the value of *b to *a
    *b = temp;  // Assign the stored value (temp) to *b
}
```

**Explanation:**

1. **Input Pointers**:
   * The function accepts two pointers, `a` and `b`, which point to integers in memory.
2. **Swapping Values**:
   * A temporary variable (`temp`) is used to store the value of `*a`.
   * The value of `*b` is then assigned to `*a`.
   * Finally, the temporary variable (`temp`) is assigned to `*b`.

**Example Usage:**

```c
#include <stdio.h>

void ft_swap(int *a, int *b);

int main()
{
    int x = 5;
    int y = 10;

    printf("Before swap: x = %d, y = %d\n", x, y);
    ft_swap(&x, &y); // Pass the addresses of x and y
    printf("After swap: x = %d, y = %d\n", x, y);

    return 0;
}
```

**Output:**

```mathematica
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
```

**Notes:**

* The function uses pointers to modify the original variables directly.
* Always ensure the pointers passed to the function are valid and not `NULL`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c01/ft_swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
