-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_memset.c
48 lines (44 loc) · 1.73 KB
/
ft_memset.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jopedro3 <jopedro3@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 12:00:19 by jopedro3 #+# #+# */
/* Updated: 2023/10/04 14:47:33 by jopedro3 ### ########.fr */
/* */
/* ************************************************************************** */
/* Function: ft_memset
Purpose: Fills a block of memory with a specified value.
How it works:
- Casts the input pointer to an
unsigned char pointer for byte-wise operation.
- Uses a while loop to traverse
through each byte of the memory block.
- Assigns the specified value (c) to each byte.
- Decreases the length (len) until it becomes zero.
- Returns the original pointer to the memory block.
*/
#include "libft.h"
void *ft_memset(void *b, int c, size_t len)
{
unsigned char *ptr;
ptr = (unsigned char *)b;
while (len > 0)
{
*ptr = (unsigned char)c;
ptr++;
len--;
}
return (b);
}
/*#include <stdio.h>
int main(void)
{
char str[20] = "Hello, World!";
// Use ft_memset to fill the first 5 bytes of str with '-'
ft_memset(str, '-', 5);
printf("Modified string: %s\n", str);
return (0);
}*/