-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_bzero.c
69 lines (58 loc) · 2.09 KB
/
ft_bzero.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sbin-jef <sbin-jef@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 15:14:35 by sbin-jef #+# #+# */
/* Updated: 2024/06/28 15:28:23 by sbin-jef ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include "libft.h"
/*#include <stdio.h>*/
void ft_bzero(void *s, size_t n)
{
unsigned char *ptr;
ptr = (unsigned char *) s;
while (n--)
{
*ptr++ = 0;
}
}
/*
int main() {
char data[10] = "Hello";
printf("Before bzero: %s\n", data);
ft_bzero(data, sizeof(data));
printf("After bzero: ");
for (int i = 0; i < sizeof(data); i++) {
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
*/
/*
The ft_bzero function works by setting each byte of a given memory area to zero.
Here’s a step-by-step explanation of how the provided code works:
ft_bzero Function
Function Signature:
void ft_bzero(void *s, size_t n)
```void *s: A pointer to the memory area that needs to be zeroed.
size_t n: The number of bytes to set to zero.```
ptr is a pointer of type unsigned char *.
It is initialized to point to the same memory location as s.
The cast to unsigned char * is necessary to perform byte-wise operations.
```unsigned char *ptr;
ptr = (unsigned char *)s;```
The while loop runs n times.
In each iteration, the byte pointed to by ptr is set to 0,
and then ptr is incremented to point to the next byte in the memory area.
```while (n--)
{
*ptr++ = 0;
}```
*/