如何使用指针算法 C 遍历分配的内存

How to loop through allocated memory with pointer arithmetic C

为了使用指针算法遍历常规 int 数组,如下所示:

 int *p;
 int arraySize = 20;

 int array[arraySize];     

 for (p = array; p< array+(sizeof(array)/sizeof(int)); p++){
    int random = rand() % 200;
    *p = random;
  }

 for (p = array; p< array+(sizeof(array)/sizeof(int)); p++){
    printf("%d\t%x\n", *p, p);
  }



 for (p = array; p<array+(sizeof(array)/sizeof(int)); p++){
        int random = rand() % 200;
        *p = random;
  }

 for (p = array; p< array+(sizeof(array)/sizeof(int)); p++){
        printf("%d\t%x\n", *p, p);
  }

但是,我要声明:

int *array = (int*) calloc(arraySize, sizeof(int));

我很困惑如何遍历动态分配的内存而不是常规的静态数组。

int *array = (int*)calloc(arraySize, sizeof(int));
int *array_end = array + arraySize;
int* ptr;
for(ptr = array; ptr < array_end; ptr++)
    printf("%p\t%d", ptr, *ptr);