I have a little problem. Why does (1) show me "p[0]=1" but (2) doesn't show me anything.Looks like you can't dynamically allocate the pointer outside of the main which seems odd to me, is that the case?
/////////
// (1) //
/////////
#include <iostream>
int main()
{
int *p = nullptr;
p = new int[4];
p[0] = 1;
printf("p[0]=%X", p[0]);
return 0;
}
// OUTPUT: p[0] = 1
/////////
// (2) //
/////////
#include <iostream>
void init(int *p)
{
p = new int[4];
p[0] = 1;
}
int main()
{
int *p = nullptr;
init(p);
printf("p[0]=%X", p[0]);
return 0;
}
// OUTPUT: