In order to loop through a regular int array with pointer arithmetic looks like such:
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);
}
However, I want to declare:
int *array = (int*) calloc(arraySize, sizeof(int));
I am very confused on how to loop through dynamically allocated memory as opposed to a regular static array.
(sizeof(array)/sizeof(int))witharraySizeand both should function as you desire.