











CompTIA
Exam Vouchers
Save money on CompTIA exams
| Question of the day
Sign up to receive
interactive practice questions
for MCSE, CompTIA
Cisco and other exams
| TestKing
Get MCSE, MCSD, CCNA, CCNP,A+, N+ and many more | * ExamSheets *
Guide for Success!
Actual Questions & Answers
MCSE, MCSD, A+ ,CCNA, CCNP
Oracle 8i, Oracle 9i Online practice tests
Certification sites Online university Online college Online education Distance learning Software forum Server administration forum Programming resources
|
|  |
| Author |
Pointers and local variables.
|
kool_gall1991
Member F

Registered: Sep 2005 Location: Jamshedpur Country: India State: Certifications: Working on: B.S.
Total Posts: 61
|
|
Pointers and local variables.
Why does the following code work:
code:
#include <stdio.h>
int add_num(int a, int b);
int main(void)
{
int sum;
sum = add_num(3, 6);
printf("\n%d\n", sum);
system ("PAUSE");
}
int add_num(int a, int b)
{
int result;
return result = a + b;
}
but the follwing will not work:
code:
int main(void)
{
swap(3, 6);
printf("\n%d\t%d\n", a, b);
system ("PAUSE");
}
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
When a function uses the "return" statement, does it disregard the deallocaqtion of the variables as in the first example?
Report this post to a moderator
|
|
10-03-05 11:48 PM
|
|
ChrisDfer
Certified Jackass M
Registered: Mar 2002 Location: Country: United States State: Certifications: Sold them all on ebay(currently selling I-NET+ & Server+ all bids considered) Working on: the really really really really really really really really really hard ones
Total Posts: 1417
|
|
It's all about the scope of the variables. In the first example when outside of the function you're not actually referencing any of the variables from within the function. You're actually only assigning the sum variable(from the main function) the value that your add_num function returns.
So any variables declared in your function will exist only inside your function. Unless of course you use global variables or some other method.
__________________
http://struthers-family.com
Report this post to a moderator
|
|
10-04-05 12:55 AM
|
|
kool_gall1991
Member F

Registered: Sep 2005 Location: Jamshedpur Country: India State: Certifications: Working on: B.S.
Total Posts: 61
|
|
ok, so when i use the 'return' statement to return a locally defined variable like 'result', that does allow the result to be returned to the calling address, right? Coz to me it looked like 'result' was only existing in the add_num function of the first progrm.
Report this post to a moderator
|
|
10-04-05 01:21 AM
|
|
ChrisDfer
Certified Jackass M
Registered: Mar 2002 Location: Country: United States State: Certifications: Sold them all on ebay(currently selling I-NET+ & Server+ all bids considered) Working on: the really really really really really really really really really hard ones
Total Posts: 1417
|
|
quote: Originally posted by kool_gall1991
ok, so when i use the 'return' statement to return a locally defined variable like 'result', that does allow the result to be returned to the calling address, right? Coz to me it looked like 'result' was only existing in the add_num function of the first progrm.
Ahh I gotcha. I see where you got confused.
Well you could have just as well done it like this(note the red text), and it would do the exact samething. However it may make it clearer as to what was going on.
#include <stdio.h>
int add_num(int a, int b);
int main(void)
{
int sum;
sum = add_num(3, 6);
printf("\n%d\n", sum);
system ("PAUSE");
}
int add_num(int a, int b)
{
return (a + b);
}
__________________
http://struthers-family.com
Report this post to a moderator
|
|
10-04-05 01:43 AM
|
|
kool_gall1991
Member F

Registered: Sep 2005 Location: Jamshedpur Country: India State: Certifications: Working on: B.S.
Total Posts: 61
|
|
ok, so in the latter program, a, b and if i choose to use it, 'result' are all local variables and are deallocated as soon as i hit the final brace. whatever i use as an argument for the 'return' gets sent back to the address where the function was called?
the problem i was really having is knowing when i have to use pointers to communicate between functions and when local variables fail to do this acceptably.
Report this post to a moderator
|
|
10-04-05 05:13 AM
|
|
dmaftei
Senior Member M
Registered: Nov 2000 Location: Country: USA State: Certifications: none Working on: none
Total Posts: 2156
|
|
quote: Originally posted by kool_gall1991
the problem i was really having is knowing when i have to use pointers to communicate between functions and when local variables fail to do this acceptably.
You use pointers when you want a function to change variables that are stored outside the function. When you pass arguments by value (as in your example) the arguments are copied to the function, and the function operates on those copies, not on the original variables.
For your swap function to work you need to do this:
code:
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a = 1;
int b = 2;
swap(&a, &b);
printf("%d %d\n", a, b);
}
To understand what's happening it helps to substitute the function call with the function body. With swap defined as above main is equivalent to:
code:
int main()
{
int a = 1;
int b = 2;
/* 'body' of swap(&a, &b) */
int temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
With the original swap (the one that takes 'int' as opposed to 'int*') main is equivalent to:
code:
int main()
{
int a = 1;
int b = 2;
/* 'body' of swap(a, b) */
int x = a;
int y = b;
int temp = x;
x = y;
y = temp;
printf("%d %d\n", a, b);
}
HTH
Report this post to a moderator
|
|
10-11-05 02:33 PM
|
|
kool_gall1991
Member F

Registered: Sep 2005 Location: Jamshedpur Country: India State: Certifications: Working on: B.S.
Total Posts: 61
|
|
thanx a lot for your help. i am finding pointers kind of hard to understand, but your post helped. again, thank u for ur help!
Report this post to a moderator
|
|
10-11-05 06:18 PM
|
|
|
Featured site: MCSE, MCSD, CompTIA, CCNA training videos
Forum Rules: Who Can Read The Forum? Any registered user or guest.
Who Can Post New Topics? Any registered user.
Who Can Post Replies? Any registered user.
Changes: Messages can be edited by their author.
Posts: HTML code is OFF. Smilies are ON. vB code is ON. [IMG] code is OFF. |
|
ExamNotes forum archive
|