不能简单地比较两个指针的值,因为指针的值只是内存地址,不能直接说明它们所指向的内存空间是否相同。可以通过使用函数memcmp()来比较指针所指向的内存空间的内容是否相同。例如:
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
int* p1 = arr1;
int* p2 = arr2;
if (memcmp(p1, p2, sizeof(arr1)) == 0) {
// pointer p1 and p2 are pointing to same memory block
}
else {
// pointer p1 and p2 are not pointing to same memory block
}
上一篇:比较不像应该那样运作