Question on pointers

Discussion in 'Mac Programming' started by cyatic, Jan 31, 2010.

  1. cyatic macrumors newbie

    Jan 31, 2010
    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,
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    They mean the same thing. I would always try to stick to the * next to the variable, though.
    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.

  3. cyatic thread starter macrumors newbie

    Jan 31, 2010
    Hi Lee,
    Thanks for the quick reply.

    int *x,y,z; 
    So this is the same as what you wrote? Also a better way of doing it?

  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Yes, that is the same. It is, IMO, a better way. Even better might be:
    int *x,*w;
    int y,z;
    Grouping your *s together and your plain ints together.

  5. cyatic thread starter macrumors newbie

    Jan 31, 2010
    Awesome! Thank you very much for your help. I appreciate you taking the time to help me. I appreciate it.


Share This Page