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

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -