C++ assigning arrays to eachother; type int* = type int works but not int = int*? -
i have question:
why throw error (cannot convert int* int[5])
int static_array[5]; int *dynamic_array; dynamic_array = new int[5]; static_array = dynamic_array;
while works fine?
int static_array[5]; int *dynamic_array; dynamic_array = new int[5]; dynamic_array = static_array; //this line changed
i guessing types int can converted int* compiler? why couldn't int converted int* compiler well?
anyone can provide actual explanation?
thanks!
this one:
int static_array[5]; int *dynamic_array = new int[5]; // 1. dynamic_array = static_array; // 2.
- dynamically allocates array , stores address of first element in pointer
dynamic_array
- overwrites address address of first element of
static_array
but one:
int static_array[5]; int *dynamic_array = new int[5]; static_array = dynamic_array;
attempts initialize array using address (thus compiler gives error invalid pointer array conversion).
so why first 1 works?
because static array (on right side) decays pointer. can not work other way, there no guarantee pointer points array.
Comments
Post a Comment