c++ - Pointers and assignment -
c++ - Pointers and assignment -
why is
int *a = new int[10];
written as
int *a; = new int[10];
instead of
int *a; *a = new int[10];
?
the way see it, in sec block of code you're saying a, pointer variable, array. in 3rd block of code you're saying thing points array. why sec create more sense third?
new int[10]
returns pointer first element in array (which of type int*
). not homecoming pointer array (which of type int(*)[10]
).
a = new int[10]
means make a
point first element of dynamically allocated array. *a
not pointer @ all. the object pointed pointer a
(which of type int
).
note if had named array object, syntax still same:
int x[10]; int* = x;
why? in c++, in cases, whenever utilize array, implicitly converted pointer initial element. here, int* = x
same int* = &x[0];
.
(there several cases array-to-pointer decay not occur, notably when array operand of &
or sizeof
operators; allow address of array , size of array, respectively.)
c++
Comments
Post a Comment