why at line 12, the expression : (char)str+3 , adds the 2nd index to the string?
for example when str="123123" , (char)str+3 is "3123" instead of "123" ?
input: 3 0 3 123123
output supposed to be: "Mybcmp result: 0" since the 3 indexes that's compared from b1="123123" and b2="123" are the same
#include <stdio.h>
#define LENGTH 128
int my_bcmp (const void *b1, const void *b2, size_t len);
int main()
{
int b1, b2, len;
char str[LENGTH];
printf("Enter length and two indexes followed by a string:\n");
scanf("%d%d%d",&len,&b1,&b2);
printf("Enter a string:\n");
fgets (str, LENGTH, stdin);
printf("len:%d strb1:%s strb2:%s str:%s\n",len,(char*)str+b1,(char*)str+b2,str);
printf("Mybcmp result: %d\n",my_bcmp((char*)str+b1,(char*)str+b2,len));
return 0;
}
int my_bcmp (const void *b1, const void *b2, size_t len)
{
int i=len;
const char* r1=(const char*)b1;
const char* r2=(const char*)b2;
while (i-- > 0)
{
if (*r1++ != *r2++)
return -1;
}
return 0;
}