-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_calloc.c
executable file
·58 lines (53 loc) · 1.52 KB
/
ft_calloc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gyong-si <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/14 14:10:19 by gyong-si #+# #+# */
/* Updated: 2023/09/14 15:15:12 by gyong-si ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
void *ft_calloc(size_t nitems, size_t size)
{
size_t total_size;
size_t i;
char *p;
total_size = nitems * size;
i = 0;
p = malloc(total_size);
if (!p)
return (NULL);
while (total_size > i)
{
p[i] = '\0';
i++;
}
return ((void *)p);
}
/*
#include <stdio.h>
int main(void)
{
int i;
int n;
int *a;
printf("Number of elements to be entered:");
scanf("%d", &n);
a = (int*)ft_calloc(n, sizeof(int));
printf("Enter %d numbers:\n", n);
for (i=0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The numbers entered are: ");
for (i=0; i < n; i++)
{
printf("%d ",a[i]);
}
free(a);
return (0);
} */