Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chad711

macrumors newbie
Original poster
Aug 17, 2011
9
0
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! :p 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[])
 
Last edited:
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
 
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.
 
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?
 
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
 
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

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.
 
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! :p
 
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.
 
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.
 
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
 
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...
 
I've posted this many times.

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

It's practically impossible to know Cocoa inside and out. There was once a discussion between two programmers about Cocoa's large APIs: one was a veteran Perl programmer, the other a Cocoa programmer. The Perl programmer grumbled about the intimidating and verbose Cocoa APIs, saying there was simply too much to remember. Bemused, the Cocoa programmer retorted: “You don't remember Cocoa; you look it up!”

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

B
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.