Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you just want to ensure that no changes are made, or do you want changes to be made but not reflected in the calling function? If the latter, you must clone because it's clear that 2 copies of the data need to exist simultaneously: one in the called function and one in the calling function. If the former, you can say that the array is const.

- eugene.yarovoi June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you are doing only read only operation in the called function then there is no problem but any change in element of the array passed by value will definitely change the array.
Another way to pass an array by value is wrap it in a structure.

- Avi June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can only wrap an array in a structure like that if the size of the array is known at compile time, though, so this isn't a very general solution.

- eugene.yarovoi June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene .... so is there any other way also?

- codez June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Aside from actually cloning the array via something like a malloc and a memcpy? Not that I can think of at this moment.

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@all thanx for d help!... i got it

- codez June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

No always the address of the first element of the array is passed,if the array is too big then why the compiler would take so much overhead of copying it.

- alexander June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

wrap the array in a struct and pass the struct by value.

- Anonymous June 25, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More