c++ - Why does this function change list value without pointer? -
if pass value function doesn't make copy of value if change within function doesn't change original unless instead pass address? stupid question, can't figure out why function changing value of list.
void array_increment(int list[],int size) { (int i=0; i<size; i++) list[i] = list[i]+1; } int main() { int c[]={3,1,0,-5,1}; array_increment(c,5); (int = 0; < 5; ++) cout << c[i] << " "; cout << endl; }
outputs 4 2 1 -4 2
there few issues @ play.
- plain arrays aren't copyable or assignable.
- function parameters such
int a[]
adjusted pointerint* a
. - array names can decay decay pointer first element
that 2nd point means that
void array_increment(int list[],int size)
is really
void array_increment(int* list,int size)
and 3rd point means can pass array c
first argument because can decay int*
.
Comments
Post a Comment