I have been working on a project and I spent the last hour trying to find the bug in my code. After closer inspection, I noticed something rather odd which has been the problem all along.
The addresses of the initial elements of my array are strangely comparing equal with memcmp()
. I have separated my code and tried a test code, and I got similar results. Can somebody explain this odd behavior?
#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[256];
char *p1 = buf;
char *p2 = buf + 3;
if (memcmp(p1, p2, sizeof(char *)) == 0) {
puts("equal...");
}
p1 = buf + 100;
p2 = p1 + 3;
if (memcmp(p1, p2, sizeof(char *)) == 0) {
puts("equal...");
}
return 0;
}