[nfbcs] Can someone please help me understand what I'm doing wrong?

Lanie Molinar laniemolinar91 at gmail.com
Fri Nov 10 03:16:13 UTC 2017


Hi. I was using an example from the textbook to try and do this. Here it 
is. Maybe this will help explain what I was trying to do. Also, someone 
else had suggested using "**", so that's what I did.

Example:https://1drv.ms/u/s!AjYDVzgAcL7ohY5Y2JRVUBAjPXgNRw


On 11/9/2017 8:55 PM, Mike Gorse wrote:
> Hi Lanie,
>
> You have
> float *pAccount;
> which defines a pointer to a float. Then
> compareBalances(account1, account2, **pAccount);
>
> The **pAccount is causing an error since pAccount is defined as a 
> float *. You're trying to dereference pAccount (which isn't what you 
> want to do here in the first place--pAccount hasn't been initialized, 
> so reading from the address pointed to by it would segfault), but, 
> regardless, dereferencing it would give a float, and the second * 
> indicates that you want to dereference the float, which you can't do, 
> since a float is not a pointer.
>
> Also, I don't think that compareBalances really does what you want it 
> to do. It accepts account1 and account2 *by value*, so, if you call 
> it, then the following would happen, more or less:
> - the value of account1 would be pushed to the stack
> - the value of account2 would be pushed to the stack
> - The function would return the address on the stack at which account1 
> or account2 are stored
> - Now that the function has returned, anything pointing to its stack 
> can no longer be considered valid. It points to memory that will be 
> written over the next time you call a function.
>
> Maybe you wanted compareBalances to take pointers as its parameters?
>
> I hope that I have explained all of this clearly; if you're learning 
> about pointers, then you need to have the foundation in terms of 
> understanding how they work and what happens underneath when a 
> function is called.
>
> On Thu, 9 Nov 2017, Lanie Molinar via nfbcs wrote:
>
>> Hi. Sorry about that. I wish there was a simpler way to do this than 
>> writing two completely different emails. I did what you suggested, 
>> but I'm still getting an error that I don't understand. You should be 
>> able to see my updated code with the link I sent earlier, but I'll 
>> add it just in case. I'll also share a link to my PuTTY log. Thanks.
>>
>> Code: https://1drv.ms/u/s!AjYDVzgAcL7ohaIYqSQEmqyOEZprZQ
>>
>> Log: https://1drv.ms/u/s!AjYDVzgAcL7ohY4Gkm1OCnzSLxHYUg
>>
>>
>> On 11/9/2017 5:04 PM, Jim Barbour via nfbcs wrote:
>>> Howdy,
>>>
>>> I'd appreciate it if you didn't cross post like this.  It makes it 
>>> harder 
>> to reply if you're not on both lists.
>>>
>>> In the compareBalances function, pAccount needs to be a float **,
>>> because you're going to assign a value to a pointer and have it seen
>>> outside the function.  So, change float * pAccount to float ** pAccount
>>> and change the pAccount = ... statements to be *pAccount = ...
>>>
>>> Hope this helps,
>>>
>>> Jim
>>>
>>> On Thu, Nov 09, 2017 at 10:38:05PM +0000, Lanie Molinar via nfbcs 
>>> wrote:
>>>> Hi, everyone. I’m working on an assignment where I’m learning how 
>>>> to use 
>> pointers. I think I almost have it but Sam’s and Sue’s balances keep 
>> showing up as 0.00 at the end. Can you please help? I’ll keep working 
>> on it, too. Thanks.
>>>>
>>>>
>>>> Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for 
>>>> Windows 
>> 10
>>>>
>>>
>>> _______________________________________________
>>> nfbcs mailing list
>>> nfbcs at nfbnet.org
>>> http://nfbnet.org/mailman/listinfo/nfbcs_nfbnet.org
>>> To unsubscribe, change your list options or get your account info for 
>> nfbcs:
>>>
>> http://nfbnet.org/mailman/options/nfbcs_nfbnet.org/laniemolinar91%40gmail.com 
>>
>>
>>
>> _______________________________________________
>> nfbcs mailing list
>> nfbcs at nfbnet.org
>> http://nfbnet.org/mailman/listinfo/nfbcs_nfbnet.org
>> To unsubscribe, change your list options or get your account info for 
>> nfbcs:
>> http://nfbnet.org/mailman/options/nfbcs_nfbnet.org/mike%40straddlethebox.org 
>>
>>





More information about the NFBCS mailing list