#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!!

Recommended Answers

All 4 Replies

The result of a cast is not an lvalue. I believe that is the case for the result of many operators.

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;
}

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.