cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
memset Implementation
memset Implementation
Jun 18, 2011 at 10:14pm UTC
lesnar56
(8)
Hi everyone, I was thinking of implementing memset.... but got stuct ... Here is the code that I wrote ...
#include<stdio.h>
void my_memset(void *ptr,int x,int no_of_bytes){
unsigned char c;
while(no_of_bytes--){
c = (unsigned char) x;
*((char *)ptr ) = c ;
((char *))ptr++;
}
}
int main(){
int *array,i;
array = (int *)malloc(10*sizeof(int));
my_memset(array,0,40);
for(i=0;i<10;i++){
printf("%d ",array[i]);
}
return 0;
}
This wont work when we want to initialize the array by 1. The problem is we are initializing each byte with 1. But other than this how to make this function generic and type cast the void pointer accordingly ?
Topic archived. No new replies allowed.