I am implementing memset()
method. Below is the code snippet:
void my_memset(void* ptr, int n, size_t size)
{
unsigned int i;
for( i = 0; i < size; ++i, ++(char*)ptr )
*(char*)ptr = n;
}
I am getting the error as:
prog.cpp: In function ‘void my_memset(void*, int, size_t)’:
prog.cpp:8: error: lvalue required as increment operand
When i change the type of ptr from void*
to int*
, it compiles successfully.
Why it is asking for lvalue?
void my_memset(void* ptr, int n, size_t size)
{
char *cptr = (char *)ptr;
char *limit = cptr + size;
for(; cptr < limit; cptr++) {
*cptr = n;
}
}
you can't increment through a cast like that, and you shouldn't want to.