C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2008, 05:14 AM   #1
Registered User
 
Join Date: May 2008
Posts: 29
Question Question about Pointers

Is this function
Code:
void sum_and_product(int *pa, *int pb)
{
  int sum = *pa + *pb;
  int diff = *pa - *pb;
 
  *pa = sum;
  *pb = diff;
}
equivalent to this one? -
Code:
void sum_and_product(int *pa, *int pb)
{
  int sum = *pa + *pb;
  int diff = *pa - *pb;
 
  pa = ∑
  pb = &diff;
}
Livnat is offline   Reply With Quote
Old 09-06-2008, 05:23 AM   #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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 09-06-2008, 09:03 AM   #3
Registered User
 
Join Date: May 2008
Posts: 54
Quote:
Originally Posted by anon View Post
...which luckily for you won't be updated in the calling code...
data.
What do you mean by this? I figured the behavior would be to change the addresses, the function returns, the memory locations held by sum and diff are discarded (they are popped off the stack) and values of the variables passed as arguments to the function are now unreliable.

Jason
jason_m is offline   Reply With Quote
Old 09-06-2008, 09:08 AM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 09-06-2008, 09:23 AM   #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   Reply With Quote
Old 09-06-2008, 06:28 PM   #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);  
   ...
x is 18, and the number 18 is passed to func in a, a is set to 7, but x is still 18 when you get back to main.

If we change it to be a pointer:
Code:
void func(int *a)
{
   *a = 7;
}

int main()
{
   int x = 18;

   func(&x);  
   ...
Now x would be 7 when we get back from func to main.

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   Reply With Quote
Old 09-07-2008, 12:42 AM   #7
Registered User
 
Join Date: May 2008
Posts: 29
Thanks!

That really cleared things up!
Livnat is offline   Reply With Quote
Reply

Tags
pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22