-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memmove.c
55 lines (50 loc) · 1.78 KB
/
ft_memmove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vinguyen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 20:58:26 by vinguyen #+# #+# */
/* Updated: 2019/10/08 20:58:32 by vinguyen ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Copies len bytes from src to dst. Two strings may overlap.
** Copy done in nondestructive manner.
** Memmove is better than memcpy because there is no overwriting
** Returns: the value of dst
*/
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
unsigned char *cdst;
unsigned char *csrc;
size_t i;
if (dst == src)
return (dst);
if (dst < src)
{
ft_memcpy(dst, src, len);
}
else
{
cdst = (unsigned char*)dst;
csrc = (unsigned char *)src;
i = 0;
while (len > 0)
{
len--;
cdst[len] = csrc[len];
}
}
return (dst);
}
/*
** The if statement will run if arr[i] = arr[i + 1] (the ad of dst < src)
** Else will run if arr[i + 1] = arr[i]. We need to do it this way, else the
** array will be altered and the original source will be destroyed
** Example:
** int arr[5] = {11, 22, 33, 44, 55}
** arr == {22, 22, 22, 22, 22} instead of arr == {22, 22, 33, 44, 55}
*/