473,387 Members | 1,603 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,387 software developers and data experts.

Pass by value/ result/ value-result/ address/ name problem

Hello there.
I had a chapter in my textbook loosely explaining passing parameters and at the end there is this exercise meant to test it.

#include <stdio.h>

int i;
int a[3];

void proc(int v){

v = v+1;
a[i]= 3;
v=v+1;
}


int main() {

for (i=1; i<=3;i++)
a[i]=0;

a[2]=10;
i=2;
proc(a[i]);

return 0;
}


I'm supposed to be able to tell the value of the elements of the array and the value of v (the formal parameter of the function) if the parameters are passed by:

a. value; possible answer: a[2] = 3, v = 12
b. result; possible answer: ?
c. value-result; possible answer: ?
d. address: a[2]=4, v = 4
e. name; possible answer: ?

The only ones I'm relatively sure of are the ones I tried to answer.

I also know that value-result usually gives results similar to address, so maybe a[2] is 4 again, but I don't know what happens to v and whether the updated value of a[2] can be seen properly.

I did not understand pass by result at all, other than knowing it can modify the actual value of the parameter (so that would make a[2]=4 again?). If anyone could shortly explain the functionality of the different ways to pass parameters based on this example, I'd be grateful.
May 31 '15 #1
6 1713
weaknessforcats
9,208 Expert Mod 8TB
This code shouldn't even compile.

First, this function:
Expand|Select|Wrap|Line Numbers
  1. void proc(int v){
  2.  
  3.    v = v+1;
  4.    a[i]= 3;
  5.    v=v+1;
  6. }
  7.  
  8.  
Here v is a calling parameter. All calling parameters have the scope of the function so that int v is a COPY of whatever was used to call the function. That means all those changes to v stay inside the function. v is destroyed when the function completes making all v logic pointless. Take v away and all that remains is a[i]= 3. It appears all the function does is change an array element value to 3.

Second, this code:
Expand|Select|Wrap|Line Numbers
  1. for (i=1; i<=3;i++)
  2.  a[i]=0;
  3. etc...
crashes the program. The array has 3 elements identified as
a[0], a[1], and a[2]. So the loop misses a[0] and allows
a[3] which is outside the array. Changing a[3] to 0 corrupts your stack and down you go.

What is it you are trying to do?
May 31 '15 #2
The for loop won't crash the program, it does compile. I have declared a as an array of three integers. It is, however, one-indexed, for whatever reason, so its elements will be a[1], a[2], a[3].

Sure, v will be destroyed, but its value before it is destroyed interested me insofar as it reflects, I think, exactly what I was trying to understand, namely what happens if you pass the parameter in all these different ways.

For instance, if you pass by address, v will be updated to that the second v = v + 1 will operate on the variable a[2] having the value 3. I guess things happen differently if you pass by value, for instance.
May 31 '15 #3
"int v is a COPY of whatever was used to call the function" - that's strictly pass-by-copy.
May 31 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
@elements will be a[1], a[2], a[3].

You don't have a choice. Array elements are indexed from 0. A 3 element array has element 0, 1, and 2.

There is no pass by address. You pass in a variable whose contents are an address. A copy is made of the variable by the function. That is, the variables used by the function are not the ones used by the calling function.

There are only two ways to pass. Pass by value and pass by reference. C has only pass by value.
May 31 '15 #5
Yes, the code is in C, but I'm supposed to simulate how it would work if all the other ways to pass a parameter could be applied. It could/should have been given in pseudocode, for all it matters. It would just have been harder to come up with snippets of code in different programming languages as an exercise.

About the indexing, I only checked the code on codepad.org, since I'm not at home and (for whatever wrong reasons) it compiled. I can see you're right, though.Thanks for the correction.
May 31 '15 #6
weaknessforcats
9,208 Expert Mod 8TB
Ah, I see.

As to the code compiling a[3] when there is no a[3] in the array, this doesn't mean the code will work as you think. The compiler doesn't know the array ends with array[2]. All it knows is that int a[3]; is a valid array definition but it doesn't keep the 3 around to check on you later. If you decide a[100] is what you want and you use the correct syntax, the compiler will not stop you.

You are permitted in C to blow your foot off.
May 31 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oleg.Ogurok | last post by:
Hi there, In an XSLT stylesheet, I need to get a value of an attribute. The problem is that I need to generate the name of the attribute on the fly. Is it possible to get the value of an...
3
by: startsmart | last post by:
Hi This is first time to write and alse it is first time to show this site my problem is i need to pass value from any input type like text for example to hidden field I tried to make a...
6
by: Luca | last post by:
Hi everyone, I'm super-newbie here... so thanx in advance for any help I shoud give a "value" to an DIV's ID that change following the name of the directory where the file is. For example the...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
1
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the...
4
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the...
9
by: JRough | last post by:
I tried to pass the $result from a mysql_query in a url like this line Header("Location:clm_historyXL.php?_result=".$result); but on the redirect location clm_history.php page I get an error on...
41
by: elham477 | last post by:
I have two pages. The first page contain a table with title and the second contain texts & selected option. I want it so when a user in second page clicks the submit button, his information is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.