0

I'm trying to use memcpy to read memory.

BYTE test[] = {0x01};
BYTE test2[] = {0x00};
memcpy (test, test2, sizeof(test));
if (test == test2){
    MessageBox::Show("Same");
}else{
    MessageBox::Show("Different");
}

Why the test and test2 always different?

Thanks for your help.

KKG
  • 299
  • 1
  • 4
  • 11

3 Answers3

3

You're comparing addresses of test and test2. Use memcmp instead.

Your code is equivalent to

if (&test[0] == &test2[0]){

Changing it to

if (memcmp(test, test2, sizeof(test)) == 0)

should work as you expected.

krzaq
  • 16,240
  • 4
  • 46
  • 61
0

Because you are testing equality for the address of the array. Try using memcmp instead.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

When the name of an array is used by itself it turns into a pointer, so you're comparing the addresses of the two arrays, not their contents.

sfjac
  • 7,119
  • 5
  • 45
  • 69