How to implement the memset function in C

1 Answer

0 votes
#include <stdio.h>

void* memset(void* s, int c, size_t n) {
	// copy c into s from s to s + n
	const unsigned char ch = c;
	unsigned char* str = (unsigned char*)s;

	for (; 0 < n; str++, n--)
		*str = ch;
	return s;
}

int main() {
	char str[32] = "c programming";

	memset(str + 2, '*', 6);

	puts(str);

    return 0;
}




/*
run:
    
c ******mming
    
*/

 



answered Dec 17, 2022 by avibootz

Related questions

1 answer 105 views
105 views asked Nov 11, 2022 by avibootz
1 answer 130 views
1 answer 175 views
5 answers 217 views
217 views asked Nov 11, 2022 by avibootz
1 answer 88 views
88 views asked Jun 10, 2025 by avibootz
1 answer 138 views
...