0
#include <stdio.h>
#include <conio.h>

void  *memset(void *dest,int value,int cnt);
void  *memset(void *dest,int val,int cnt)
{
	void *start=dest;

	while(cnt--)
	{
		*((char *)dest)++=(char)val;
		
	}
	return start;
}
int main()
{
	char arr[]={1,2,3,4};
	memset(arr,0,4);
	printf("%s",arr);
	

	_getch();
	return 0;
}

error : ++ need lvalue
I know how to resolve this,but I want to know the reason for that?
I think this is a special case when we use a cast operator.
compiler : VS 9

Cheers!!

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by MrNoob
0

Why do you insist on redefining functions...Try this

#include <stdio.h>

void  memseta(void *dest,int val,int cnt)
{ 	
	while(cnt--)
	{
		*((char *)dest++) = (char)val;
		
	}
}
int main()
{
	char arr[]={1,2,3,4,0};
	memseta(arr,103,4);
	printf("%s\n",arr);
	
	return 0;
}

Edited by gerard4143: n/a

0

with void i think you need to cast according to the type at first not set a void ptr to void ptr

0

here

#include <stdio.h>
void  *memset(void *dest,int val,int cnt)
{
	char *p=(char*)dest;
	while(cnt) {
		*p++=(val+'0');
		cnt--;
	}
	*p='\0';
	return p;
}
int main()
{
	char arr[]={1,2,3,4};
	memset(arr,0,4);
	printf("%s",arr);
	getchar();
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.