pharaoh fortune slot
ExamNotes.net
Welcome, Guest. Please login or register.
November 23, 2017, 11:54:15 PM

Login with username, password and session length
* Home Help Search Login Register
+  ExamNotes.net
|-+  General discussions
| |-+  Programming Forum
| | |-+  Pointers and local variables.
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Pointers and local variables.  (Read 3892 times)
kool_gall1991
Member
Jr. Member
**
Offline Offline

Posts: 61

0


View Profile WWW
« on: October 03, 2005, 07:48:47 PM »

Why does the following code work:

Code:

#include

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?
Logged

ChrisDfer
Certified Jackass
Hero Member
*****
Offline Offline

Posts: 1430

2


View Profile
« Reply #1 on: October 03, 2005, 08:55:31 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.
Logged
kool_gall1991
Member
Jr. Member
**
Offline Offline

Posts: 61

0


View Profile WWW
« Reply #2 on: October 03, 2005, 09:21:14 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.
Logged

ChrisDfer
Certified Jackass
Hero Member
*****
Offline Offline

Posts: 1430

2


View Profile
« Reply #3 on: October 03, 2005, 09:43:48 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

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);

}
Logged
kool_gall1991
Member
Jr. Member
**
Offline Offline

Posts: 61

0


View Profile WWW
« Reply #4 on: October 04, 2005, 01:13:11 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.
Logged

dmaftei
Senior Member
Hero Member
*****
Offline Offline

Posts: 2156

1


View Profile
« Reply #5 on: October 11, 2005, 10:33:37 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
Logged

BSEE, MSCS
www.maftei.net
kool_gall1991
Member
Jr. Member
**
Offline Offline

Posts: 61

0


View Profile WWW
« Reply #6 on: October 11, 2005, 02:18:41 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!
Logged

Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!