just as the title says how to compare between memory addresses's contents?,
my function will get two memory pointers and i want to compare their first "len" from what they're pointing to

i'm getting here invalid use of void * when i compile

int my_bcmp (const void *r1, const void *r2)
{
  int i;

  for(i=0; i<LENGTH && i<len; i++)
    if(*b1+i != *b2+i)
        return -1;
  return 0;
}

@rproffitt cuz im trying to figure what's wrong with this code the function always returns -1

#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];
  scanf("%d%d%d%s",&len,&b1,&b2,str);
  printf("Mybcmp result: %d\n",my_bcmp((char*)str+b1,(char*)str+b2,len));

  return 0;
}

int my_bcmp (const void *r1, const void *r2, size_t len)
{
  int i;

  for(i=0; i<LENGTH && i<len; i++)
  {
    if((const char*)r1+i != (const char*)r2+i)
        return -1;
  }
  return 0;
}

When crafting wheels my advice is to look at existing wheels. Memory comparison is a stock function so let's see how it's done and be done with minor functions in a jiffy.

Now there are prior discussions about the use of const void so I didn't write a thing about that.

If you mean't about lines 5 and 17 just saying that wasn't the issue at all i was just trying different inputs and forgot to change it back before posting,
I've seen the actual memory comparison in the library so here it is
But still i don't understand why the fgets eats the first 2 characters of the string, for example:
input: 3 0 3 123123
output suppsoed to be: 0
since (char)str+0 and (char)str+3 from those indexes when comparing 3 indexes forward it should be the same string
but (char*)str+3 gets to be 3123 instead of 123, why?

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

fgets? That's not the topic from the top. If you want to start another discussion about fgets() problems, do that.

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.