|
Home > Archive > Programming Forum > October 2005 > Pointers and local variables.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Pointers and local variables.
|
|
| kool_gall1991 2005-10-03, 6:48 pm |
| 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? | |
| ChrisDfer 2005-10-03, 7:55 pm |
| 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. | |
| kool_gall1991 2005-10-03, 8:21 pm |
| 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. | |
| ChrisDfer 2005-10-03, 8:43 pm |
| 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);
} | |
| kool_gall1991 2005-10-04, 12:13 am |
| 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. | |
| dmaftei 2005-10-11, 9:33 am |
| 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 | |
| kool_gall1991 2005-10-11, 1:18 pm |
| 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! |
|
|
|
|