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 96 views
96 views asked Nov 11, 2022 by avibootz
1 answer 124 views
1 answer 162 views
5 answers 196 views
196 views asked Nov 11, 2022 by avibootz
1 answer 84 views
84 views asked Jun 10, 2025 by avibootz
1 answer 132 views
...