In the following program, I am allocating a new array on the stack and one on the heap. The type of the stack allocated array is int
and the type of the heap allocated array is int*
. However, when passed into PrintArray
they appear to both be int*
. Why is the type of the stack allocated array not explicitly written as int*
on declaration?
#include <iostream>
void PrintArray(int* array, int count) {
for (int i = 0; i < count; i++)
std::cout << *(array + i) << std::endl;
}
int main(int argc, char* argv[]) {
const int count = 5;
int stackAllocated[count];
int* heapAllocated = new int[count];
for (int i = 0; i < count; i++) {
stackAllocated[i] = i;
*(heapAllocated + i) = i;
}
PrintArray(stackAllocated, count);
PrintArray(heapAllocated, count);
delete[] heapAllocated;
}