Melian wrote:
Code:
int bcmp (const void *s1, const void *s2, int len)
{
for (int i = 0; i<len; i++)
{
if ((const char*) (*s1)!=(const char*) (*s2)) return 1;
s1++;
s2++;
}
return 0;
}
Всё!
Твой код вообще не компилируется ( (const char*) (*s1) вместо *((const char*)s1) приводиш значение ячейки памяти к указателю

), к тому-же ты используеш долгую операцию приведения типа в цикле и в целом не эффективно выходит.
Я б сделал так:
Code:
int bcmp(const void *s1,const void *s2,int len)
{
unsigned char *m1 = (unsigned char*)s1;
unsigned char *m2 = (unsigned char*)s2;
if (s1 != s2)
for(int i=0;i < len; i++)
{
if (m1[i] != m2[i])
return 1;
}
return 0;
}
int main()
{
std::cout << bcmp("Hello world!", "Hello world!",12) << std::endl;
std::cout << bcmp("Hello world!", "Not hello world!",12) << std::endl;
int a=1;
int b=2;
std::cout << bcmp(&a,&b,4) << std::endl;
struct s {
int _x,_y;
s(int x, int y)
{
_x = x, _y = y;
}
} s1(1,1),s2(2,2);
std::cout << bcmp(&s1,&s2,sizeof(s)) << std::endl;
return 0;
}