473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Array: Pass by value... Possible?

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
--------------------------------------------------------------------------------
I want the world, and I want it... NOW!
Nov 13 '05 #1
2 4656


Rohit Malaviya wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 13 '05 #2
In article <77**************************@posting.google.com >, Rohit Malaviya wrote:
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.

--
Neil Cerutti
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen Williams | last post by:
Hey i've got bunch of arrays of tick boxes, each array contains somewhere between 5 and 20. What I want to do is write a function that returns the captions of every ticked tick box in an array...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
7
by: Morgan | last post by:
I have read much posts on the argument but no one clearly says if this operation is possible or not. Simply I have a routine which reads from a text file some integer arrays (1 or 2D). The...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
10
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.