![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 29
| Code: void sum_and_product(int *pa, *int pb)
{
int sum = *pa + *pb;
int diff = *pa - *pb;
*pa = sum;
*pb = diff;
}
Code: void sum_and_product(int *pa, *int pb)
{
int sum = *pa + *pb;
int diff = *pa - *pb;
pa = ∑
pb = &diff;
}
|
| Livnat is offline | |
| | #2 | |
| The larch Join Date: May 2006
Posts: 3,222
| No, the second changes the addresses the pointers point to, which luckily for you won't be updated in the calling code (since they are addresses to local variable which go out of scope). Otherwise, it looks somewhat strange that you'd want to completely lose the original data.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 | |
| Registered User Join Date: May 2008
Posts: 54
| Quote:
Jason | |
| jason_m is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,809
| pa and pb are values passed to the function. As such, any changes made to those values are discarded at the end of the function. In other words, you can change what a pointer points to in a function, but not where a function points, if you want the changes seen in the calling code. |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: May 2008
Posts: 54
| Right. I need a 1 hour wait rule after getting out of bed before being able to post :-p. It occurred to me how wrong my post was when I got in the shower and I almost bolted back out to the computer to edit it before anyone could see it, but oh well. So yes, the parameters pa and pb are two newly created pointers to ints setup when sum_and_product(...) is called. They are not the original arguments supplied to sum_and_product, but rather hold the addresses of the variables supplied as those arguments. Changing the addresses held by pa and pb is fine, now they point to sum and diff. So, if you were to change sum later in the function, *pa would produce this new value. However, you've lost any means by which to update the arguments supplied in the calling function. Jason |
| jason_m is offline | |
| | #6 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| To change ANYTHING in a function, you need it's address (or in other words, it needs to be a pointer to what you want to change). E.g Code: void func(int a)
{
a = 7;
}
int main()
{
int x = 18;
func(x);
...
If we change it to be a pointer: Code: void func(int *a)
{
*a = 7;
}
int main()
{
int x = 18;
func(&x);
...
If you want to change what a pointer points to, you would have to pass a pointer to a pointer. And finally, if you ever take an address of a local variable, and use that address after you have returned from the function that had the local variable, don't expect it to work - that variable is no longer the variable you took the address of - it is either free space on the stack or being used for something else. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #7 |
| Registered User Join Date: May 2008
Posts: 29
| Thanks! That really cleared things up! |
| Livnat is offline | |
![]() |
| Tags |
| pointers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array pointers question? | ben2000 | C Programming | 4 | 07-26-2007 01:31 AM |
| A question on Pointers & Structs | FJ8II | C++ Programming | 4 | 05-28-2007 10:56 PM |
| simple pointers question | euphie | C Programming | 4 | 05-25-2006 01:51 AM |
| Very stupid question involving pointers | 7smurfs | C Programming | 6 | 03-21-2005 06:15 PM |
| Quick Question Regarding Pointers | charash | C++ Programming | 4 | 05-04-2002 11:04 AM |