📌ft_calloc
(size * count)의 크기만큼 malloc하고 모두 0으로 초기화 하는 함수
void *ft_calloc(size_t count, size_t size);
➕ 매개변수 (Parameters)
count
: malloc할 크기size
: malloc 할 한칸의 사이즈
➕ 반환값 (Return)
void *
: 0을 초기화한 주소반환
➕ 설명 (Description)
- 다른 함수에서 ft_calloc을 통해서 공간을 초기화하다가 오류가 났어도 0을 반환할 뿐이지 return (0)으로 함수를
아예 끝내는 것이 아니기 때문에 return (ft_calloc); 을 해야한다
➕ 코드 (Code)
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
char *tmp;
tmp = malloc(count * size);
if (!tmp)
return (0);
ft_memset(tmp, 0, count * size);
return (tmp);
}
📌ft_strdup
src를 크대로 복사하여 그 위치를 반환하는 함수
char *ft_strdup(const char *src);
➕ 매개변수 (Parameters)
src
: 복사할 원본 문자열의 주소
➕ 반환값 (Return)
char *
: 복사한 문자열의 주소 반환
➕ 설명 (Description)
- (src_len + 1)만큼 malloc해서 마지막에 '\0'을 넣어주어야 한다.
➕ 코드 (Code)
#include "libft.h"
char *ft_strdup(const char *src)
{
char *tmp;
int src_len;
int i;
i = 0;
src_len = ft_strlen(src);
if (!(tmp = malloc(src_len + 1)))
return (0);
while (src[i] != '\0')
{
tmp[i] = src[i];
i++;
}
tmp[i] = 0;
return (tmp);
}
반응형
'➰ 코딩 부트캠프 > 42 seoul' 카테고리의 다른 글
[0 Circle] Libft - ft_strtrim, ft_split (0) | 2021.01.04 |
---|---|
[0 Circle] Libft - ft_substr, ft_strjoin (0) | 2021.01.04 |
[0 Circle] Libft - ft_isalpha, ft_isdigit, ft_isalnum, ft_isascii, ft_isprint, ft_toupper, ft_tolower (0) | 2021.01.04 |
[0 Circle] Libft - ft_atoi (0) | 2020.12.24 |
[0 Circle] Libft - ft_strnstr, ft_strncmp (0) | 2020.12.24 |