본문 바로가기
프로그래밍/C

C ] memset 함수 구현

by eteo 2022. 10. 9.

 

void *my_memset(void *s, int c, int len)
{
    unsigned char *p = (unsigned char *)s;

    while (len > 0)
    {
        *p = c;
        p++;
        len--;
    }

    return (s);
}

 

 

 

#include <stdio.h>

void *my_memset(void *s, int c, int len)
{
    unsigned char *p = (unsigned char *)s;

    while (len > 0)
    {
        *p = c;
        p++;
        len--;
    }

    return (s);
}

int main()
{
    char str[13] = "hello world!";

    for (int i = 0; str[i] != '\0'; i++)
    {
        printf("%c", str[i]);
    }
    printf("\n");

    my_memset(str, 0, 13);

    for (int i = 0; i < 13; i++)
    {
        printf("%d", str[i]);
    }
    printf("\n");

    return 0;
}