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

cyatic

macrumors newbie
Original poster
Jan 31, 2010
3
0
hello everyone,
I'm learning objective-c at the moment and am having a little bit of trouble. I think I'm grasping it a little but am confused because of two examples. The first has the pointer on the left side. The second example has the pointer on the right side. Does having * on either side make a difference, or does it mean somthing else when on each side respectively? Is it on the right side when used with types and left side when used on identifyers? By the way, the book I'm using is "Objective-c for Dummies." I wasn't sure if it was a typo, but after looking on the internet I've seen the * used on both sides. Thanks in advance.

Example 1:
int anInterger = 42;
int *anIntergerPointer = &anInterger;




Example 2:

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

budget vacationBudgetEurope;
budget vacationBudgetEngland;

void spendDollars(budget* theBudget, double dollars);
void chargeForeignCurrency(budget* theBudget, double foreignCurrency);

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

vacationBudgetEurope.exchangeRate = 1.2500;
vacationBudgetEurope.budget = 1000.00;
double numberDollarsInEuroland = 100;
double numberEuros = 100;

vacationBudgetEngland.exchangeRate = 1.5000;
vacationBudgetEngland.budget = 2000.00;
double numberDollarsInPoundland = 100;
double numberPounds = 100;

spendDollars(&vacationBudgetEurope, numberDollarsInEuroland);
chargeForeignCurrency(&vacationBudgetEurope, numberEuros);

spendDollars(&vacationBudgetEngland, numberDollarsInPoundland);
chargeForeignCurrency(&vacationBudgetEngland, numberPounds);

return 0;


void spendDollars(budget* theBudget, double dollars) {
theBudget->budget -= dollars;
}

void chargeForeignCurrency(budget* theBudget, double foreignCurrency) {
theBudget->exchangeTransaction = foreignCurrency*theBudget->exchangeRate;
theBudget->budget -= theBudget->exchangeTransaction;
}


Thanks again,
Cyatic
 
They mean the same thing. I would always try to stick to the * next to the variable, though.
Code:
int* x,y,z;
Means that x is an int *, and y and z are ints. If you stick the * next to the variable it makes this clearer.

-Lee
 
They mean the same thing. I would always try to stick to the * next to the variable, though.
Code:
int* x,y,z;
Means that x is an int *, and y and z are ints. If you stick the * next to the variable it makes this clearer.

-Lee

Hi Lee,
Thanks for the quick reply.

Code:
int *x,y,z;

So this is the same as what you wrote? Also a better way of doing it?

-Cyatic
 
Code:
int *x,y,z;

So this is the same as what you wrote? Also a better way of doing it?

-Cyatic

Yes, that is the same. It is, IMO, a better way. Even better might be:
Code:
int *x,*w;
int y,z;

Grouping your *s together and your plain ints together.

-Lee
 
Awesome! Thank you very much for your help. I appreciate you taking the time to help me. I appreciate it.


-Cyatic
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.