Are passing char array by (char* ar) and (char ar[ ]) same? c++ -


this question has answer here:

i new c++ , need bit confused if 2 ways thought different same thing. here code of passing down character array function.

     // first style use    void functionptr( char *ptr ) // pass reference                 {                      // put random code                  strcpy(ptr,"by_pointer");                   cout<<ptr[0]; // random thing                 } 

// second style use    void functionval( char val[] ) // pass value                  {                   ... code here                 } 

this confusion have. thought first 1 pass reference , changes in variable in function change original passed variable , thought second traditional pass value way function creates own value , uses modify not affect original passed variable.

things here not expected me. in both cases change made in function variable reflected in original passed variable.
can please explain me how thing works. please dont harsh on me still learning on own.

stopping read que , me out.

they both exactly same.

when passed function parameter, val decays pointer type: it's type char*.

in neither case deep copy of array taken.

whether use val[i], ptr[i], *(val + i), or *(ptr + i) access array elements purely matter of personal taste.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -