C++ Pointer Assignment Clarification -
this question has answer here:
what difference when have example,
int var1, *ptr; ptr = &var1; // pointer ptr copies address of var1, hence ptr points var1? int var1, *ptr; ptr = var1; // ptr points var1, not have address of var1, can not change value of address var1? int *var1, *ptr; *ptr = *var1; // copy value of var1 location pointed ptr?
are comments correct ?
the second (ptr = var1
) , third (*ptr = *var1
) options wrong.
in second case, asking ptr
point address written in var1
. i.e. var1
integer value interpreted address. not want, , cause compiler error or warning.
in third case trying dereference not pointer (*var1
). compiler error.
Comments
Post a Comment