C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 30th, 2003, 11:04 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
assigning pointer in function argument

I have a class called Quantity and a list<Quantity*> called gWorkspace.

I'd like to write a function which will take a pointer to a Quantity and a string as arguments. The function would then search the list and assign the pointer to point to the Quantity located using the string. My current implementation for the function is:

bool findVar(Quantity* poutVar, string nameString)
{
// iterator for workspace
list<Quantity*>::iterator wsiter;



for (wsiter = gWorkspace.begin();
wsiter != gWorkspace.end();
++wsiter)
{ // loop through workspace
if ((*wsiter)->getName() == nameString)
{ // found a match by name
poutVar = *wsiter; // point output to this quantity
return true; // success
}
}

return false;
}

and my code to call the function is:

pfoundVar = new Quantity;
resultVar = findVar(pfoundVar,name);
....

When i run the function and provide a name which produces a match the function returns true and assigns the pointer in the function correctly (i.e. poutVar is a valid pointer just after poutVar = *wsiter) but when control passes back to the calling code the pointer is invalid and attempting to access pfoundVar generates a segfault. any ideas?? thanks

Reply With Quote
  #2  
Old March 31st, 2003, 12:09 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
Remember that in C/C++, arguments are passed by value. Whatever you do to the argument within the function, it does not change the variable that was passed to it. That is why when we want to change a variable's value outside the function, we need to pass a pointer to that variable and dereference it within the function.

That is exactly what your program needs to do. As it now stands, you pass in a pointer to a Quantity. With that pointer, you can change the Quantity that it is pointing to. However, any changes you make to the pointer itself will only have an effect within the function itself and will not affect the pointer outside of the function.

For what you want to do, you need to pass a pointer to a pointer to a Quantity. Regard:
Code:
bool findVar(Quantity** poutVar, string nameString)
{
...
*poutVar = *wsiter; // point output to this quantity
...


Now you are able to change what the Quantity** is pointing to.

Of course, that is assuming that your usage of wsiter is correct.

BTW, I've worked with functions that required three or four levels of referencing (ie, 3 or 4 asterixes were needed).

Last edited by dwise1_aol : March 31st, 2003 at 12:13 AM.

Reply With Quote
  #3  
Old March 31st, 2003, 07:14 AM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks a lot. that was a very clear and helpful reply!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > assigning pointer in function argument


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway