# New to programming, question about code in this book.

Discussion in 'Mac Programming' started by Chad711, Aug 17, 2011.

1. Aug 17, 2011
Last edited: Aug 17, 2011

Joined:
Aug 17, 2011
#1
I'm reading a book for beginners to programming. It's on Obj-C.

The part I'm reading about now is on functions and data structures.

This code is straight from the book. The author ask me to type it as I read along to better understand. It sure does help. I'm stuck on something here.

In this function one of the arguements is "theBudget". I don't see where he ever had me declare this variable and so I'm thinking it is not a variable at all. Could someone explain what this is, also explain what is going on with
Code:
`"budget* theBudget.`
I know budget is the struct type but it looks like it's multiplying it with theBudget.

Here is code...not sure how much to post to help you so will start from top all the way to my code in question. (LOL I'm typing this on my Desktop and just tried to copy and paste from my mac to desktop! Need coffee!) I have 3 monitors going right now so honest silly mistake! Going to post this then just edit the post on my mac...one moment please.

Here we go..

Code:
```#import <Foundation/Foundation.h>

typedef struct {
float exchangeRate;
double budget;
//double euroTransaction;
double exchangeTransaction;
} budget;

//budget vacationBudget;
budget vacationBudgetEurope;
budget vacationBudgetEngland;

//void spendDollars (double dollars);
//void chargeEuros (double euros);
void spendDollars(budget* [COLOR="RoyalBlue"]theBudget[/COLOR], double dollars);
void chargeForeignCurrency(budget* theBudget, double foreignCurrency);

int main (int argc, const char * argv[])
```

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#2
Is this "Objective-C for Dummies" By Neal Goldstein?

EDIT: If so, you may want to go back and re-read the section called "Accessing Data With Pointers."

B

3. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#3
The * indicates that the type of theBudget is a pointer to a budget struct. The * is a switch hitter in C. It can be used for binary multiplication, unary dereference of a pointer, or as part of a declaration to indicate a pointer type. This doesn't change in objective-c, but it's used frequently with objects since they are always accessed via a pointer.

-Lee

Joined:
Aug 17, 2011
#4
Yes it is. I will do that thanks.

Edit: Thanks Lee, I some what understand but being new I will go back and read that section again that other poster mentioned.

Thank you for the help.

Joined:
Aug 17, 2011
#5
Makes sense now. I just noticed that I had the * next to the struct variable instead of the pointer. However the code ran fine with out error. So I guess that doesn't matter?

6. ### mydogisbox macrumors member

Joined:
Jan 16, 2011
#6
Correct. The * can go on either the datatype or the variable name. Be careful when declaring multiple things on one line though.

int* a, b, c, d; <- confusing

int *a;
int b, c, d; <-less confusing

7. ### ehoui macrumors regular

Joined:
Jan 27, 2011
#7
I would humbly suggest never declaring multiple things on one line. Is it possible? Yes. Are there exceptions? Sure. Is it a practice that leads to messy code? Usually.

Joined:
Aug 17, 2011
#8
I started out taking C# and Java last year but only for two months. My full time career (Mortgages) and my daughter, I just couldn't find the time for two classes too. Well now I've ventured off into this language and plan on reading a lot to try and self teach myself. Problem is I'm no math wiz and I hate to read! However I've told myself I would apply myself more this time around (reason I got a MacBook Pro). Crossing fingers!

I recall my teacher saying the same thing about declaring multiple variables on one line makes room for errors.

I'm sure I will run into a lot more problems before that is ever a problem for me!

Joined:
Aug 17, 2011
#9
Thought I would post this in here instead of making a new topic. Hopefully someone reads it!

I'm reading over the NMNumber class chapter and I want to see if I understand something. Here is the code I'm asking about...

Code:
```- (void) spendDollars: (NSNumber*) dollars;

- (void) spendDollars: (NSNumber*) dollars{

budget -= [dollars doubleValue];
}```
To my understanding NSNumber assigns a value like int, chart, short, long, etc in a method. My question is, does the type that is assigned not given until "doubleValue" is read? Does that make sense? Sorry if I'm not phrasing terms correctly as I'm still learning.

### Staff Member

Joined:
Aug 16, 2005
Location:
New England

Joined:
Aug 17, 2011
#11
I did that before coming here. I think I understand it but not 100%. In this book I'm reading every once and awhile it seems he puts things into laymen terms but other times I feel like he skipped something that I thought was important to understand or he barely scratched the surface enough for me to grasp it.

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#12
NSNumber is essentially an object representation of a generic number with fewer restrictions than the standard C types.

When you need to, e.g. to operate on them, you can convert them to the standard C types, but in many cases you are better off leaving it in the object form. You can stuff it in NSArray and NSDictionary collections...

B

Joined:
Aug 17, 2011
#13
Still reading the same book, my first book in Obj C. I find myself forgetting what some things are or what they do or I just don't think I understand somethings. Is this common to forget? Do you read back over it or just keep going through the book and then once you're done start over?

Seems like any time I start reading a book I get things pretty easy at first and then as I keep reading things start to cluster in my brain and it seems that I start forgetting things the more I read...

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#14
I've posted this many times.

There's an anecdote at the beginning of Cocoa in a Nutshell that bears repeating.

The important thing is to learn where to look things up.

B

Joined:
Aug 17, 2011
#15
Understood. What about this book though? This is "Obj C for Dummies" so it's the basics I'm reading. Shouldn't I be absorbing the basics?

One thing that helped me out when doing C# for a few months was being able to stop the program debugger after each line ran to see the output. How do I do that in Xcode.

You've been a great help so far! Thank you

Joined:
Aug 16, 2005
Location:
New England
#16