Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparison – passing array and pointer to a function

I understand that when we pass a pointer to a function by-value, the pointer gets copied and the function only operates with a copy which stores the address that is the same as the original’s, so it can be used to alter the value at the original address.

Now, if we pass an array to a function by-value, people say that this is the same as with the pointer. They also say that I can treat the array as-if it is a pointer.

But, to me, it looks much different! Because we pass an array by-value, the function will again make a copy. I am guessing that it copies the first array element. And this element does not store any address! So we lose the connection. What gets copied here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t pass an array by value, only by pointer or reference. If you try to pass it by value, the array will decay into a pointer to the 1st element, and the pointer will be passed by value.

What is array to pointer decay?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading