C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 15th, 2013, 03:34 PM
c0ldhand c0ldhand is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 c0ldhand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 42 sec
Reputation Power: 0
Question 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;
}

Reply With Quote
  #2  
Old March 15th, 2013, 03:40 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 20 m 37 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to 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.

Reply With Quote
  #3  
Old March 15th, 2013, 03:59 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
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.

Reply With Quote
  #4  
Old March 15th, 2013, 04:05 PM
c0ldhand c0ldhand is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 c0ldhand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 42 sec
Reputation Power: 0
Red face 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.

Reply With Quote
  #5  
Old March 16th, 2013, 05:12 PM
eramit2010 eramit2010 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Location: India
Posts: 23 eramit2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Can somebody explain how we got 3, 15, 45 for x, y, z?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap