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.

  1. plain arrays aren't copyable or assignable.
  2. function parameters such int a[] adjusted pointer int* a.
  3. 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

Popular posts from this blog

php - Using str_replace to translate a MySQL Table in html -

asp.net mvc - Cannot display error message on Editor or EditorFor -

SQL Server - MyCTE query based on 24 hour period (next day) -