The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Can somebody explain how we got 3, 15, 45 for x, y, z?
Discuss Can somebody explain how we got 3, 15, 45 for x, y, z? in the C Programming forum on Dev Shed. Can somebody explain how we got 3, 15, 45 for x, y, z? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 15th, 2013, 03:34 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 34 m 42 sec
Reputation Power: 0
|
|
Can somebody explain how we got 3, 15, 45 for x, y, z?
#include <stdio.h>
#include <stdlib.h>
int stuff(int a, int *b);
int things(int L, int *M);
int main(){
int x = 3, y = 5, z = 0;
z = stuff(x, &y);
printf("%d\t%d\t%d\n", x, y, z);
return 0;
}
int stuff(int a, int *b){
if (a % 2) {
*b = things(a, b);
return a * *b;
}else
return a + *b;
}
int things (int L, int *M){
*M = L * *M;
return *M;
}
|

March 15th, 2013, 03:40 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
Remember the &y in stuff mean y is sent by reference so whatever happens to the formal parameter in the function affects the value of the actual parameter.
|

March 15th, 2013, 03:59 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
The simplest method for determining how this code works is to step it in your debugger while watching the variables. That will be far more instructive than any explanation that might be given here.
Besides that, you have left the question rather too far open, necessitating any answer to exhaustively describe the execution of the code; something the debugger will be far better for, and which to be honest is a) too much work, b) in danger of doing your homework for you. Instead you might explain why you thing the results should perhaps be something else, and why you think that. That way the answer can put you straight on your specific misunderstanding.
|

March 15th, 2013, 04:05 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 34 m 42 sec
Reputation Power: 0
|
|
This is my explanation; would appreciate any thoughts
main function:
On the stack > we got x = 3, y =5, z =0
main calls stuff and passes x and &y
x =3
&y is the address of y; ex. 0x1000; y = 5;
stuff function
On the stack > we got a = 3, b = &y, hence *b = 5
3 modules 2 = 1 condition is true
stuff calls things
things function
On the stack > we got L = 3, M = &y, hence *M = 5
*M = L * *M
*M = 3 * 5 = 15
things function return 15 to stuff; *b is now 15
stuff function return a + *b = 3 * 15 = 45 to main
Now main have x = 3, y =15, z = 45;
Is my logic right?
Quote: | Originally Posted by bullet Remember the &y in stuff mean y is sent by reference so whatever happens to the formal parameter in the function affects the value of the actual parameter. |
|

March 16th, 2013, 05:12 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Location: India
Posts: 23
Time spent in forums: 11 h 27 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by c0ldhand main function:
On the stack > we got x = 3, y =5, z =0
main calls stuff and passes x and &y
x =3
&y is the address of y; ex. 0x1000; y = 5;
stuff function
On the stack > we got a = 3, b = &y, hence *b = 5
3 modules 2 = 1 condition is true
stuff calls things
things function
On the stack > we got L = 3, M = &y, hence *M = 5
*M = L * *M
*M = 3 * 5 = 15
things function return 15 to stuff; *b is now 15
stuff function return a + *b = 3 * 15 = 45 to main
Now main have x = 3, y =15, z = 45;
Is my logic right? |
agree with this logic its correct
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|