Array: Pass by value... Possible?

R

Rohit Malaviya

Hi,
I was asked this question at a recent interview... it goes like this:


Suppose I want to manipulate an array inside a function (say, sort)...
but I do not want the changes to be reflected in the calling function.
How do I do it?

Now folks, do not tell me that I should copy the array to temp
array... using a 'for' loop... thats not the solution.

Can we somehow send the array (and not base address) such that only
values get transferred?

Thanks in Advance...

- Rohit
 
K

Karl Heinz Buchegger

Rohit said:
Hi,
I was asked this question at a recent interview... it goes like this:

Suppose I want to manipulate an array inside a function (say, sort)...
but I do not want the changes to be reflected in the calling function.
How do I do it?

Now folks, do not tell me that I should copy the array to temp
array... using a 'for' loop... thats not the solution.

Can we somehow send the array (and not base address) such that only
values get transferred?

wrap the array in a struct
A struct gets copied when passed by value and so will the
array inside it.
 
N

Neil Cerutti

Hi,
I was asked this question at a recent interview... it goes like this:


Suppose I want to manipulate an array inside a function (say, sort)...
but I do not want the changes to be reflected in the calling function.
How do I do it?

Now folks, do not tell me that I should copy the array to temp
array... using a 'for' loop... thats not the solution.

Pass each element as parameters to a variadic function.

It's a little verbose. ;-)
I want the world, and I want it... NOW!

I don't want the world, I just want your half.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top